Compress or Zip a File - Windows Phone Universal Apps - C#

1.3k views Asked by At

I need to compress file for send in HttpWebRequest for a server. I try to use external classes of C# but doesn't work in Universal Apps. I try this with a original classes that can I use in Universal apps and this is my code:

 //SycroZipFile and UploadFile are StorageFile type.
 using (Stream writeStream = await SyncroZipFile.OpenStreamForWriteAsync())
 {
     using (Stream readStream = await UploadFile.OpenStreamForReadAsync())
     {
         await readStream.CopyToAsync(writeStream);
     }
 }

But the ZipFile is corrupt and doesn't work, any one can help me for compress file in Windows Phone Universal Apps? Thanks a lot, sorry for my english and Thanks in advance!

1

There are 1 answers

0
Merlí Escarpenter Pérez On BEST ANSWER

Finally I could solve my problem! If you are programmer of Windows Phone Universal Apps have a problems with a external classes to zip a files, you can use my code with original classes from Visual that can use in Universal Apps:

//The UploadFile and SyncroZipFile are StorageFile's.
using (Stream StreamToUploadFile = await UploadFile.OpenStreamForWriteAsync())
{
    using (Stream StreamToSyncroZipFile = await SyncroZipFile.OpenStreamForWriteAsync())
    {
        using (ZipArchive UploadFileCompressed = new ZipArchive(StreamToSyncroZipFile, ZipArchiveMode.Update))
        {
            ZipArchiveEntry NewZipEntry = UploadFileCompressed.CreateEntry(UploadFileName);

            using (Stream SWriter = NewZipEntry.Open())
            {
                 await StreamToUploadFile.CopyToAsync(SWriter);
            }
        }
    }
}

This code work perfectly for my, and finally my ZipArchive it's not corrupt and I can open it! Good Luck!