Zip control does not decompress correctly

3 posts, 1 answers
  1. koby or
    koby or avatar
    2 posts
    Member since:
    Jun 2012

    Posted 13 Jun 2012 Link to this post

    Hello,

    I am trying the ZIP control for silverlight, but i keep having an issue.
    I am trying to Compress and Uncompress a stream which is getting read from a WAV file.
    the issue is that the size of the stream before compression and after decompression is different. this causes the uncomressed stream to be invalid and therefore not to be played by the basic media element of silverlight.

    I am attaching a sample of code to demonstrate the zip and unzip methods that i use.

    string wavFilePath = @"C:\1\cheer.wav";
    string wavFileNewPath = @"C:\1\cheerNew.wav";
    string zipFilePath = @"c:\1\123.zip";
     
    private void btnCompress(object sender, RoutedEventArgs e)
    {
        FileStream fs = new FileStream(wavFilePath, FileMode.Open);
        SizeBefore.Text = fs.Length.ToString();
        using (ZipPackage zipFile = ZipPackage.CreateFile(zipFilePath))
        {
             zipFile.AddStream(fs, "cheer.wav", ZipCompression.Default, DateTime.Now);
             zipFile.Close(true);
         }       
    }
     
    private void btnUncompress(object sender, RoutedEventArgs e)
    {
         using (ZipPackage zipFile = ZipPackage.OpenFile(zipFilePath, FileAccess.Read))
         {
             ZipPackageEntry zipEntry = zipFile.ZipPackageEntries[0];
             Stream strm = zipEntry.OpenInputStream();
             byte[] Bytes = new byte[strm.Length];
             strm.Read(Bytes, 0, (int)strm.Length);
             using (FileStream fs = new FileStream(wavFileNewPath, FileMode.Create))
             {
                  fs.Write(Bytes, 0, Bytes.Length);
                  fs.Close();               
             }
        }
     }

    I wanted to also note that I did encounter the decompress example which is online here. but it does not help me since dealing with string will not give me the expected result.

    Thank you in advance for nay help about this...

    kob490.
  2. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 13 Jun 2012 Link to this post

    H Koby,

    Thank you for sending your code samples. They helped us better understand the issue. It is caused by the logic implemented in the btnUncompress event handler and especially by this lines of code:
    byte[] Bytes = new byte[strm.Length];
    strm.Read(Bytes, 0, (int)strm.Length);

    When working with a ZipPackageEntry object, it is important to note that it gives information about the size of the compressed stream, along with the uncompressed size of the original stream. Also the OpenInputStream() method opens the compressed stream and therefore it returns a stream with size that matches the compressed size of the entry. This is why it is better to replace the above lines with these:
    byte[] Bytes = new byte[zipEntry.UncompressedSize];
    strm.Read(Bytes, 0, (int)zipEntry.UncompressedSize);

    Basically as the zipEntry.UncompressedSize property holds the size of the uncompressed stream, you need to use its value to define the size of the byte array where you will decompress the ZipEntry.

    Also, when we tested your scenario on our side we found an issue with the ZipLibrary. It seems that it cannot properly compress media files. If you test the attached solution with a non-media file, it will work as expected. However, when we tested it with .wmv and .wav files, we realized that the size of the created .zip file is larger than the original size of the media file. This is why I logged this issue in our PITS where you can track its progress. I also updated your Telerik account as your report helped us find the issue.

    Greetings,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. koby or
    koby or avatar
    2 posts
    Member since:
    Jun 2012

    Posted 13 Jun 2012 Link to this post

    Yap that explains it, thank you. :)
Back to Top