使用C#,> net 3.5,VS2008压缩文本文件 [英] ZIP a Text file using C#, >net 3.5 , VS2008

查看:97
本文介绍了使用C#,> net 3.5,VS2008压缩文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要创建一个Windows服务应用程序,以便从预定义位置压缩文本文件并将其存储在数据库中。我必须在Vs2008和framework3.5环境中这样做。我是使用Windows服务的新手。我正在尝试添加DotnetZip库并将iconic.zip添加到我的应用程序中。但是在添加引用之后我也没有在我的.cs文件中获得Using Iconic.zip,如许多示例所示。请通过分享有价值的建议和代码来帮助我解决这个问题。

Need to create a windows service app to ZIP a text file from a predefined location & store it n a database. I have to do this in Vs2008 & framework3.5 environment. I am a novice in using "windows services" . I am trying to add "DotnetZip" library & add reference "iconic.zip" to my app. But after adding reference also I am not getting "Using Iconic.zip" in my .cs file as shown in many examples. Please help me in solving this issue by sharing valuable suggestions & code.

推荐答案

使用SharpZipLib(开源),你可以从他们的样本中得到很多有用的例子。

http://www.icsharpcode.net/OpenSource/SharpZipLib/ [ ^ ]
Use SharpZipLib(Open Source), you get many working examples there too from their samples.
http://www.icsharpcode.net/OpenSource/SharpZipLib/[^]


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;


namespace trail2zip
{
    public partial class trail2zip : ServiceBase
    {
        Timer timer;
        string path1 = @"E:\zipped files\New Text Document.txt";
        string path2 = @"E:\output\filezipname.zip";
        string path3 = @"E:\zipped files\R_23122015.txt";


        int timerInterval = 60000;

        public trail2zip()
        {
            InitializeComponent();
            timer = new Timer();
            timer.Elapsed+=new ElapsedEventHandler(this.timer_Elapsed);
            timer.Interval = timerInterval;
            timer.Enabled = true;

        }

        protected override void OnStart(string[] args)
        {
            timer.Start();
        }

        protected override void OnStop()
        {
            timer.Stop();
            timer.SynchronizingObject = null;
            timer.Elapsed -= new ElapsedEventHandler(this.timer_Elapsed);
            timer.Dispose();
            timer = null;


        }
        public void timer_Elapsed(object sender, ElapsedEventArgs e)
        {

            ZipFile z = ZipFile.Create(path2);       //(filezipname);
            z.BeginUpdate();
            z.Add(path1);
            z.Add(path3);
            z.CommitUpdate();
            z.Close();


        }
    }
}



以上代码成功压缩预定义的文本文件文件夹位置。现在我想以这样一种方式修改这段代码:'如果找到文件夹中的任何文件(在运行时给出文本文件的名称),应该上传数据&它应该是拉链的,&应删除旧文件。我可以这样做,如果我没有运行时文本文件名,但在运行时也这样做..希望有人会帮我这个。


the above code is successfully zipping a text file from a predefined folder location. Now i want to modify this code in such a way that 'any file in the folder if found (giving the name of text file in run time)the data should be uploaded & it should be zipped, & the old file should be deleted. I can do this if i dont have runtime text file names, but doing the same in run time.. hope someone will help me with this.


这篇关于使用C#,> net 3.5,VS2008压缩文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆