locked
Problem with reading compressing files RRS feed

  • Question

  • I am using the following code to compress files...

     CompressAlgorithm Algorithm = CompressAlgorithm.Lzms;
                using (var originalInput = await file.OpenReadAsync())
                using (var compressedOutput = await sfile.OpenAsync(FileAccessMode.ReadWrite))
                using (var compressor = new Compressor(compressedOutput.GetOutputStreamAt(0), Algorithm, 0))
                {
                     var bytesCompressed = await RandomAccessStream.CopyAsync(originalInput, compressor);
                    var finished = await compressor.FinishAsync();
                    tb.Text += "\nCompressed Successfully";
                    tb.Text += String.Format("\nCompressed the selected file of size {0} bytes into {1} bytes\n", bytesCompressed, compressedOutput.Size);

                } 

                                       Compression works fine but after compressing when i try to open the compressed file it is corrupted... Is there a way to compress the files without corrupting it???

    Tuesday, August 20, 2013 4:49 PM

Answers

  • Hi meesun,

    The sample here provide you a way to compress/decompress the file, while running the sample the document you choosed will have both a compressed file and decompressed file locates in the Document Library.

    You can read compressed data and write the decompress data to a new file to see what's inside. I don't think the compression will corrupt the file, but instead you may not use the correct decompress method.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 21, 2013 5:06 AM
    Moderator
  • If you're trying to create a zip file, this is not the correct API. The API you use is for just compressing a random set of bytes.

    Instead look at the ZipArchive class: http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx


    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net

    Wednesday, August 21, 2013 7:26 AM
  • Hi meesun,

    As far as I know there are no API like the Compressed state in Desktop dev.

    But you can catch the exception that thrown by decompress method, if an exception thrown, that means the file is not compressed, at least not by following algorithms  (the InvalidAlgorithm will be returned )::

    Mszip | mszip - The compressor will use the MSZIP compression algorithm.
    Xpress | xpress - The compressor will use the XPRESS compression algorithm.
    XpressHuff | xpressHuff - The compressor will use the XPRESS compression algorithm with Huffman encoding.
    Lzms | lzms - The compressor will use the LZMS compression algorithm.

    By the way, you can also test ZipArchive and the exception can be "NotSupportedException" or "InvalidDataException"

    Best regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, August 21, 2013 8:48 AM
    Moderator

All replies

  • Hi meesun,

    The sample here provide you a way to compress/decompress the file, while running the sample the document you choosed will have both a compressed file and decompressed file locates in the Document Library.

    You can read compressed data and write the decompress data to a new file to see what's inside. I don't think the compression will corrupt the file, but instead you may not use the correct decompress method.

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 21, 2013 5:06 AM
    Moderator
  • If you're trying to create a zip file, this is not the correct API. The API you use is for just compressing a random set of bytes.

    Instead look at the ZipArchive class: http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx


    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net

    Wednesday, August 21, 2013 7:26 AM
  • Thank u Jamles and Morten!

    May i know how to find out whether a file is in compressed state??

    Wednesday, August 21, 2013 7:30 AM
  • Hi meesun,

    As far as I know there are no API like the Compressed state in Desktop dev.

    But you can catch the exception that thrown by decompress method, if an exception thrown, that means the file is not compressed, at least not by following algorithms  (the InvalidAlgorithm will be returned )::

    Mszip | mszip - The compressor will use the MSZIP compression algorithm.
    Xpress | xpress - The compressor will use the XPRESS compression algorithm.
    XpressHuff | xpressHuff - The compressor will use the XPRESS compression algorithm with Huffman encoding.
    Lzms | lzms - The compressor will use the LZMS compression algorithm.

    By the way, you can also test ZipArchive and the exception can be "NotSupportedException" or "InvalidDataException"

    Best regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, August 21, 2013 8:48 AM
    Moderator