0

I am trying to convert an entire azure blob storage folder and its contents to a zip file .Inside this folder ,I have different types of files eg, .txt,.mp3,.zip files .But once the folder is converted to zip file I noticed that all the .zip file types got corrupted,.How can I prevent my zip files from corrupted. I am using Ionic.Zip library to generate zip files

Here is the code I am using .Here I am able to generate and download the zip file successfully with all other filetypes except the inner zip files.

var allFiles = directory.ListBlobs(new BlobRequestOptions { UseFlatBlobListing = true }).Where(x => x.GetType() == typeof(CloudBlockBlob)).Cast<CloudBlob>();

string xyzblob = directory.Uri.ToString().TrimEnd('/');
var dBlob = blobClient.GetBlobReference(xyzblob);
byte[] fileBytes = null;
fileBytes = dBlob.DownloadByteArray();
foreach (var file in allFiles)
{
    using (var fileStream = new MemoryStream(fileBytes))
    {
        var entryName = file.Uri.ToString().Replace(directory.Uri.ToString(), "");
         zipOutputStream.PutNextEntry(entryName);
         fileStream.Seek(0, SeekOrigin.Begin);
         int count = fileStream.Read(fileBytes, 0, fileBytes.Length);
         while (count > 0)
         {
             zipOutputStream.Write(fileBytes, 0, count);

            count = fileStream.Read(fileBytes, 0, fileBytes.Length);
            if (!Response.IsClientConnected)
            {
                break;
            }
            Response.Flush();
        }
        fileStream.Close();
     }
}
zipOutputStream.Close(); 

More details

I am downloading a folder ,."myFolder" and its contents from azure blob as a zip file eg, myfolders.zip.

Here is how the file structure inside "myFolder" /azure blob

MyFolder/mymusic/ test.mp3

MyFolder/mytext/ newtext.txt

MyFolder/MyZipfiles/ myzip.zip

My code I posted above will generate a zip all the contents of the folder to create "MyFolder.zip" and will download automatically .Now if you unzip "MyFolder.zip" file , due to some reason , the myzip.zip is getting corrupted.If I try to open myzip.zip file ,its showing a message "windows cannot open the folder ,the compressed zipped folder "myzip.zip" is invalid"

Please help me find a solution so that the .zip files wont get corrupted

I tried to download to stream ,but same results.,The inner zip files are getting corrupted.all other file types are in good shape. zipOutputStream.PutNextEntry(entryName); destBlob.DownloadToStream(zipOutputStream);

Ashkan S
  • 10,464
  • 6
  • 51
  • 80
Millar
  • 1,095
  • 6
  • 16
  • 34

2 Answers2

0

I am assuming you already tried downloading one of those zip files and opening it, right?

If that is the case, one thing I would suggest is to eliminate the intermediate fileBytes array completely. Using fileBytes as the buffer to fileStream and then reading from fileStream to fileBytes might be the culprit. On the other hand, you start from offset 0 and write to the beginning of fileBytes anyway, so it might be working just fine.

In any case, a more efficient solution is; you can call PutNextEntry and then call the blob object's DownloadToStream method by passing in the zip stream itself. That would simply copy the entire blob directly into the zip stream without having to manage an intermediate buffer.

Serdar Ozler
  • 3,752
  • 17
  • 24
  • I tried PutNextEntry and then call the blob object's DownloadToStream method by passing in the zip stream itself ,but no luck. – Millar Oct 05 '13 at 19:19
  • If I manually open the azure blob folder the and the existing zip file inside my folder are in good shape. But if I download them programmatically, its getting corrupted – Millar Oct 05 '13 at 19:31
0

When it starts to pick the .zip file ,I added BlobReference to .zip file and this resolved the issue

dBlob = blobClient.GetBlobReference(entryName.EndsWith(".zip") ? file.Uri.ToString() : xyzblob);                    
zipOutputStream.PutNextEntry(entryName);
dBlob.DownloadToStream(zipOutputStream);
Millar
  • 1,095
  • 6
  • 16
  • 34