locked
how to create a zip file and extract it RRS feed

  • Question

  • i want to create a C# components and using System.IO.Compression to create or exteact the zip file but:

    "The ZipFile class provides convenient static methods for working with zip archives. To use these methods, you must reference the System.IO.Compression.FileSystem assembly in your project. The System.IO.Compression.FileSystem assembly is not available for Metro style apps. Therefore, the ZipFile class and ZipFileExtensions class (which is also in the System.IO.Compression.FileSystem assembly) are not available in Metro style apps. "

    so i can't reference the System.IO.Compression.FileSystem assembly,is there any other way to operate zip file in javascript app?

       
    Wednesday, May 2, 2012 6:47 AM

Answers

  • i can extract the zip file use C# components :

    private async void extract ()
            {
                StorageFolder SF = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("SampleBook");
                Stream stream = await SF.OpenStreamForReadAsync("SampleBook.zip");

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read))
                {
                    try
                    {
                        foreach (ZipArchiveEntry entry in archive.Entries)
                        {
                            StorageFile newFile = await CreateFile(SF, entry.FullName);
                            Stream newFileStream = await newFile.OpenStreamForWriteAsync();
                            Stream fileData = entry.Open();
                            byte[] data = new byte[entry.Length];
                            await fileData.ReadAsync(data, 0, data.Length);
                            newFileStream.Write(data, 0, data.Length);
                            await newFileStream.FlushAsync();
                        }
                    }
                    catch(Exception x)
                    {
                    }

                }
            }

            private static async Task<StorageFile> CreateFile(StorageFolder dataFolder, string fileName)
            {
                return await dataFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting).AsTask();
            }

    Wednesday, May 2, 2012 10:18 AM

All replies

  • i can extract the zip file use C# components :

    private async void extract ()
            {
                StorageFolder SF = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("SampleBook");
                Stream stream = await SF.OpenStreamForReadAsync("SampleBook.zip");

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read))
                {
                    try
                    {
                        foreach (ZipArchiveEntry entry in archive.Entries)
                        {
                            StorageFile newFile = await CreateFile(SF, entry.FullName);
                            Stream newFileStream = await newFile.OpenStreamForWriteAsync();
                            Stream fileData = entry.Open();
                            byte[] data = new byte[entry.Length];
                            await fileData.ReadAsync(data, 0, data.Length);
                            newFileStream.Write(data, 0, data.Length);
                            await newFileStream.FlushAsync();
                        }
                    }
                    catch(Exception x)
                    {
                    }

                }
            }

            private static async Task<StorageFile> CreateFile(StorageFolder dataFolder, string fileName)
            {
                return await dataFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting).AsTask();
            }

    Wednesday, May 2, 2012 10:18 AM
  • Hi,

    Thank you for sharing your solutions and experience here. It will be very beneficial for other community members who have similar questions.

    If you have any difficulty in future programming, we welcome you to post in forums again.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Thursday, May 3, 2012 2:24 AM
    Moderator