none
Zip / Unzip using ISharpZipLib (#Zip)

    Question

  • Hi all,

    I need do this:

    1. Zip any file (byte[] or FileStream) and I get de byte[] (byte array) compressed

    file1.txt => byte[] file1 uncompress => compress using SharpZipLib => byte[] file1compress

    2. Unzip byte[] for get byte[] (byte array) uncompressed

    byte[] file1compress => uncompress using SharpZipLib => byte[] file1 UNcompress => file1.txt

    I dont want compress in filesystem, not use temporary directory or temp files.

    Any sample code, please ??

    Thanks. in advanced

    Tuesday, June 08, 2010 10:39 AM

All replies

  • Zip Files :

    public static bool ZipFiles(string[] files,string ZipPath,string ZipFileName,string Password)
     {
      try
      {
      ZipFile zip = ZipFile.Create(ZipPath + Path.DirectorySeparatorChar + ZipFileName + ".zip");
      zip.BeginUpdate();
      foreach (string s in files)
      {
       zip.Add(s,Path.GetFileName(s));
      }
      if (!string.IsNullOrEmpty(Password))
       zip.Password = Password;
      zip.CommitUpdate();
      zip.Close();
      return true;
      }
      catch (Exception)
      {
    
      return false;
      }
      
     }
     }
    public static void UnZip(string SrcFile, string DstFile, int BufferSize)
    {
      FileStream fileStreamIn = new FileStream
    		(SrcFile, FileMode.Open, FileAccess.Read);
      ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
      ZipEntry entry = zipInStream.GetNextEntry();
      FileStream fileStreamOut = new FileStream
    		(DstFile + @"\" + entry.Name, FileMode.Create, FileAccess.Write);
      int size;
      byte[] buffer = new byte;
      do
      {
        size = zipInStream.Read(buffer, 0, buffer.Length);
        fileStreamOut.Write(buffer, 0, size);
      } while (size > 0);
      zipInStream.Close();
      fileStreamOut.Close();
      fileStreamIn.Close();
    }

    Tuesday, June 08, 2010 10:47 AM
  • Mister, thanks,

    in UnZip method,  byte[] buffer = new byte; not compiles.

    Too, in Zip method zip.Add(s,

    Path.GetFileName(s)); not compiles

    Using ISharpZipLib 0.85.4.369

    Any solution??

     

    In my case,

    not is UnZip(string SrcFile ...   is UnZip(byte[] file ... 

    and

    not ZipFile zip = ZipFile.Create(ZipPath + Path.DirectorySeparatorChar + ZipFileName + ".zip");

    is ZipFile.Create(MEMORYSTREAM);



    I want not use FileSystem (files...) only byte[]-memorystream

    Thanks in advanced.

    Tuesday, June 08, 2010 11:02 AM
  • Mister, thanks,

    in UnZip method,  byte[] buffer = new byte; not compiles.

    Too, in Zip method zip.Add(s,

    Path.GetFileName(s)); not compiles

    Using ISharpZipLib 0.85.4.369

    Any solution??

     

    In my case,

    not is UnZip(string SrcFile ...   is UnZip(byte[] file ... 

    and

    not ZipFile zip = ZipFile.Create(ZipPath + Path.DirectorySeparatorChar + ZipFileName + ".zip");

    is ZipFile.Create(MEMORYSTREAM);



    I want not use FileSystem (files...) only byte[]-memorystream

    Thanks in advanced.

    Hi,

    for the Path you need to add" using System.IO;" to your Classheader

    Unzip File :

    byte[] buffer = new byte[zipInStream.Length];

     

    For Your propose , just replace the Files wit a Memorystream 

     

    like 

    MemoryStream ms = new MemoryStream();
            ZipFile zip = ZipFile.Create(ms);
            zip.BeginUpdate();
            foreach (string s in files)
            {
              zip.Add(s,Path.GetFileName(s));
            }
            if (!string.IsNullOrEmpty(Password))
              zip.Password = Password;
            zip.CommitUpdate();
            zip.Close();
    //Add your SaveLogic here

    Greeetings

    • Proposed as answer by Pawel Warmuth Tuesday, June 08, 2010 12:40 PM
    Tuesday, June 08, 2010 11:46 AM
  • Use the GZipStream class in System.IO.Compress namespace. Pass in a stream and get the Zipped stream.

    Giuseppe

    Tuesday, June 08, 2010 12:56 PM
  • Mister Pawel,

    for me , not compile this:

     foreach (string s in files)
            {
              zip.Add(s,Path.GetFileName(s)); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            }

    greetings

    Wednesday, June 09, 2010 3:21 PM
  • Hi,

    What the error is it? Could you please post the whole code you are working with?

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, June 11, 2010 3:08 AM
  • The code:

    MemoryStream ms = new MemoryStream();

    ZipFile zip = ZipFile.Create(ms);

    zip.BeginUpdate();

    foreach (string s in files)

    {

    zip.Add(s,Path.GetFileName(s)); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    }

    if (!string .IsNullOrEmpty(Password)) zip.Password = Password;

    zip.CommitUpdate();

    zip.Close();

    //Add your SaveLogic here

    Wednesday, June 16, 2010 6:31 AM