Results 1 to 4 of 4

Thread: How to Zip Files Using C#

  1. #1
    Join Date
    Feb 2009
    Posts
    26

    How to Zip Files Using C#

    Hello , Can You Tell me How Can i Zip files Using C# , Please provide me the code and solution to this problem , also Can you Tell me that can i use Zip Files in C# Thanks in advance

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to Zip Files Using C#

    You Can Use these code For Using Zip Files in C#

    private void zip(string[] args)

    {

    //Create an empty zip file

    byte[] emptyzip = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    FileStream fs = File.Create(args[1]);

    fs.Write(emptyzip, 0, emptyzip.Length);

    fs.Flush();

    fs.Close();

    fs = null;

    //Copy a folder and its contents into the newly created zip file

    Shell32.ShellClass sc = new Shell32.ShellClass();

    // returns a Folder of args parameter

    Shell32.Folder SrcFlder = sc.NameSpace(args[0]);

    Shell32.Folder DestFlder = sc.NameSpace(args[1]);

    Shell32.FolderItems items = SrcFlder.Items();

    DestFlder.CopyHere(items, 0);

    //Ziping a file using the Windows Shell API creates another thread where the zipping is executed.

    //This means that it is possible that this console app would end before the zipping thread

    //starts to execute which would cause the zip to never occur and you will end up with just

    //an empty zip file. So wait a second and give the zipping thread time to get started

    System.Threading.Thread.Sleep(1000);

    }

    and then u call this method

    string[] str = new string[2];

    str[0] = zipFolderPath; //zipFolderPath where you want to attachment zip file is added

    str[1] = _startUpPath + "\\" + "Attachment.zip"; //path contain zip file

    zip(str); //call zip method.

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: How to Zip Files Using C#

    Hello , You Can Use thes Code

    FileStream sourceFile = File.OpenRead(@"C:\sample.xml");
    FileStream destFile = File.Create(@"C:\sample.zip");

    GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);

    try
    {
    int theByte = sourceFile.ReadByte();
    while (theByte != -1)
    {
    compStream.WriteByte((byte)theByte);
    theByte = sourceFile.ReadByte();
    }
    }
    finally
    {
    compStream.Dispose();
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to Zip Files Using C#

    How to Zip/Unzip files using C#.NET

    using System.IO.Compression;
    using System.IO;
    Example data to run ZipFile function:
    sourceFile = @"D:\"
    destinationFile = @"C:"
    Private bool ZipFile(string sourceFile, string destinationFile)
    {
    using (FileStream oldFile = File.OpenRead(sourceFile)
    using (FileStream newFile = File.Create(destinationFile)
    using (GZipStream compression = new GZipStream(newFile, CompressionMode.Compress))
    {
    byte[] buffer = new byte[1024];
    int numberOfBytesRead = oldFile.Read(buffer, 0, buffer.Length);
    while (numberOfBytesRead > 0)
    {
    compression.Write(buffer, 0, numberOfBytesRead);
    numberOfBytesRead = oldFile.Read(buffer, 0, buffer.Length);
    }
    compression.Close();
    }
    }
    Example data to run UnZipFile function :
    sourceFile = @"C:\backup\standards.zip
    destinationFile = @"C:\backup\standards.pdf"
    Private bool UnZipFile(string sourceFile, string destinationFile)
    {
    using(FileStream compressFile = File.Open(sourceFile,FileMode.Open))
    using (FileStream uncompressedFile = File.Create(destinationFile)
    using (GZipStream compression = new GZipStream(compressFile,
    CompressionMode.Decompress))
    {
    int data = compression.ReadByte();
    while(data != -1)
    {
    uncompressedFile.WriteByte((byte) data);
    data = compression.ReadByte();
    }
    compression.Close();
    }
    }

Similar Threads

  1. Replies: 7
    Last Post: 04-05-2012, 12:37 PM
  2. Replies: 2
    Last Post: 23-02-2012, 01:15 PM
  3. Replies: 6
    Last Post: 12-07-2011, 12:17 AM
  4. Replies: 2
    Last Post: 10-06-2010, 08:21 PM
  5. Replies: 2
    Last Post: 10-06-2009, 11:16 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,729,636.68506 seconds with 16 queries