none
Generating a zipOutputstream from a folder having other .zip files in it

    Question

  • 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 the zip file successfully with all other filetypes *except the 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();

    Friday, October 04, 2013 9:25 PM

Answers

All replies

  • Hi Devk1,

    In the first read your code looks good. Let me ask you few questions so that we can get more details of your scenario and it would help you get accurate answer

    1) When you say Zip file are corrupted, do you mean that you can't unzip it?

    2) Where are you storing your zip file? Are you again uploading your zip files to Blob storage?

    3) What would happen if you have only one file to Zip? Does one file zip also look corrupted?

    Is your question already answered in forum - http://social.msdn.microsoft.com/Forums/en-US/1cd60f18-cab1-4beb-8a6d-7240a2fae512/generating-zip-files-in-azure-blob-container-getting-corrupted?forum=windowsazuredata ?


    Regards,
    Ojas Maru ( My blog )

    Friday, October 04, 2013 10:49 PM
  • Hi Devk1,

    In the first read your code looks good. Let me ask you few questions so that we can get more details of your scenario and it would help you get accurate answer

    1) When you say Zip file are corrupted, do you mean that you can't unzip it?

    2) Where are you storing your zip file? Are you again uploading your zip files to Blob storage?

    3) What would happen if you have only one file to Zip? Does one file zip also look corrupted?

    Is your question already answered in forum - http://social.msdn.microsoft.com/Forums/en-US/1cd60f18-cab1-4beb-8a6d-7240a2fae512/generating-zip-files-in-azure-blob-container-getting-corrupted?forum=windowsazuredata ?


    Regards,
    Ojas Maru ( My blog )


    1) When you say Zip file are corrupted, do you mean that you can't unzip it? --I can unzip the inner zip file

    2) Where are you storing your zip file? Are you again uploading your zip files to Blob storage?

    zip file is generated on the flow and downloads it to users machine

    3) What would happen if you have only one file to Zip? Does one file zip also look corrupted?

    All files types worked fine ,but only the inner zip file is getting corrupted

    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 , the .txt files and .mp3 and all file types are fine, but 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"




    • Edited by devk1 Saturday, October 05, 2013 2:43 AM
    Saturday, October 05, 2013 2:03 AM
  • Hi devk,

    What is size of your "myfolders.zip" which download from blob? Is it not null? And Is your "myzip.zip" not repeatedly compressed ? Do you try download "myzip.zip" and decompressed it? If you have new finding, please let me know.

    Thanks

     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 08, 2013 7:15 AM
    Moderator
  • DownloadByteArray does not allow you to download an entire "folder". You can only download a single blob with this method. So please call it in the foreach loop to download each blob individually, and then construct the zip file. Make sure you pass a blob's URI, not a container's URI. Otherwise you won't get the blob's content.
    Tuesday, October 15, 2013 7:49 AM