compress stream to stream which is .zip format?

Aug 12, 2011 at 7:00 AM

Excuse me , if I want to compress stream to stream which is .zip format?

One thing I know is GZipStream Class is .gz format.  I just want .zip format replace .gz format.Others like conclusion of http://dotnetzip.codeplex.com/discussions/268506   .

public byte[] Zip(string ToCompress)
{
 using (Stream input = StringToMemoryStream(ToCompress))
 {
  MemoryStream raw = new MemoryStream();
  using (Stream compressor = new GZipStream(raw, CompressionMode.Compress))—— just want .zip format replace by .gz format
  {
   byte[] buffer = new byte[2048];
   int n;
   while ((n = input.Read(buffer, 0, buffer.Length)) != 0)
   {
    compressor.Write(buffer, 0, n);
   }
   byte[] bytes = raw.ToArray(); 

   return bytes;
  }
 }
}

 So now Which I should to choose?  ZipOutputStream and ZipInputStream?How to do it?

 

 

Coordinator
Aug 14, 2011 at 12:50 AM

ZipOutputStream.  Check the documentation for examples.

Aug 16, 2011 at 9:15 AM

1) First problem, compress file to file:

string filesToZip = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\new.xml";       //pathname :program files/test/new.xml

string outputFileName =  Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\new.zip";   //pathname :program files/test/new.zip

public void Zipup()
{
 using (ZipFile zip = new ZipFile())
 {
  zip.AddFile(filesToZip);
  zip.Save(outputFileName);
 }
}

But this a problem, when I decompression this zip, there are two folders, just like program files/test/new.xml. But I want when I decompression this zip,there is just new.xml. What I can do????

2) Second problem, compress stream to stream, last time you say I can "ZipOutputStream.  Check the documentation for examples".

           if (filesToZip.Count == 0)
            {
                return;
            }

           using (var raw = File.Open(outputFileName, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var output = new ZipOutputStream(raw))
                {
                    foreach (string inputFileName in filesToZip)
                    {
                        output.PutNextEntry(inputFileName);
                        using (var input = File.Open(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Write))
                        {
                            byte[] buffer = new byte[2048];
                            int n;
                            while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                output.Write(buffer, 0, n);
                            }
                        }
                    }
                }
            }

filesToZip and outputFileName are all filenames, what I can do to change it to compress stream to stream????

Coordinator
Aug 21, 2011 at 5:30 PM

File.Open returns a stream.  in your code, replace File.Open() with the method that returns your stream.