Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried the below code for zipping a text file using ICsharpCode library. Anyone could help me out in accomplishing the task. Am novice in 'wondows services' ..am able to register a service..& instal it successfully ..yet the task is being not completed.


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;


C#
namespace createzip
{
    public partial class createzip : ServiceBase
    {
        public createzip()
        {
            InitializeComponent();
        }


C#
protected override void OnStart(string[] args)
        {

            if (args.Length < 2)
            {

                //DirectoryInfo d = new DirectoryInfo("E:\\zipped files\\R_23122013");
                /* I guess my mistake is lying in below 2 lines */
                ZipFile z = new ZipFile("E:\\zipped files\\R_23122013");
                Console.WriteLine(z);//"E:\\R_23122013"
                return;
            }
            if (!Directory.Exists(args[0]))
            {
                Console.WriteLine("cannot find directory '(0)'", args[0]);
                return;
            }
            try
            {
                string[] filenames = Directory.GetFiles(args[0]);
                using (ZipOutputStream s = new ZipOutputStream(File.Create(args[1])))
                {
                    s.SetLevel(9);
                    byte[] buffer = new byte[4096];
                    foreach (string file in filenames)
                    {
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        using (FileStream fs = File.OpenRead(file))
                        {
                            int sourcebytes;
                            do
                            {
                                sourcebytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourcebytes);
                            }
                            while (sourcebytes > 0);
                        }
                    }
                    s.Finish();
                    s.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception during processing {0}", ex);
            }


        }


        protected override void OnStop()
        {
        }
    }
}
Posted
Comments
[no name] 22-Aug-13 7:34am    
Since it is supposed to be a windows service, get rid of the UI stuff that you will never see and add logging to your code so you can trace what your code is doing.
sri67 22-Aug-13 7:37am    
Sorry. can u pls elaborate? I didnt get any UI stuff in this!!
[no name] 22-Aug-13 7:51am    
Elaborate on what? Get rid of the user interface code. It's a windows service and as a windows service there is no Console to write to.
sri67 22-Aug-13 8:02am    
ya thank you..I removed tht line from my code.Yet not working.
[no name] 22-Aug-13 8:04am    
"Not working" tells us absolutely nothing. Did you add any logging into your code to catch the exceptions and so you can trace your code execution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900