Why this doesn't work

Mar 12, 2010 at 11:57 PM

I am doing this from Azure so I can not write to a disk file, getting a ZipStream with password (this is OK), how can i create a new stream from the original one without the password successfully, ZipOutputStream at the end and also the MemoryStream are 57 bytes long.

        public System.IO.MemoryStream GetDecryptedZipFile(System.IO.Stream blobZipStream, string zipPassword)
        {

            Ionic.Zip.ZipInputStream zis = new Ionic.Zip.ZipInputStream(blobZipStream);
            zis.Password = zipPassword;

            MemoryStream ms = new MemoryStream();
                         
           
            Ionic.Zip.ZipOutputStream zos = new Ionic.Zip.ZipOutputStream(ms);
            Ionic.Zip.ZipEntry zipEntry;
         
            byte[] buffer = new byte[2048];
            while ((zipEntry = zis.GetNextEntry()) != null)
            {
                   
                zos.PutNextEntry(zipEntry.FileName);
                int bytesRead = 0;

                while ((bytesRead = zis.Read(buffer, 0, buffer.Length)) > 0)
                {

                    zos.Write(buffer, 0, bytesRead);
                    zos.Flush();
                }


            }
            zos.Flush();
            zos.Close();
            zis.Close();
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }

Mar 13, 2010 at 12:24 AM

Found a way that works. 

       public System.IO.MemoryStream GetDecryptedZipFile(System.IO.Stream blobZipStream, string zipPassword)
        {

            Ionic.Zip.ZipInputStream zis = new Ionic.Zip.ZipInputStream(blobZipStream);
            zis.Password = zipPassword;

           
           
            Ionic.Zip.ZipEntry zipEntry;

            Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile();
           

            byte[] buffer = new byte[2048];
            while ((zipEntry = zis.GetNextEntry()) != null)
            {

                int bytesRead = 0;
                System.IO.MemoryStream ms = new MemoryStream();
                while((bytesRead = zis.Read(buffer,0,buffer.Length)) > 0)
                {
                    ms.Write(buffer,0,bytesRead);
                }

                zip.AddEntry(zipEntry.FileName, ms.ToArray());


            }
           
            zis.Close();

            System.IO.MemoryStream outputStream = new MemoryStream();
            zip.Save(outputStream);
            outputStream.Seek(0, SeekOrigin.Begin);

            return outputStream;
        }