How to unzip a file in IsolatedStorage in a Windows Phone 8 app?

In my application, I am trying to download about 180 small audio files. I tried BackgroundTransferService, but it does not look stable with a lot of small files. So now I download the ZIP of all these audio files and want to extract them into the "audio" folder. I tried a method in this thread:

How to unzip files in Windows Phone 8

But I get this error: 'System.IO.IOException' occurred in mscorlib.ni.dll...in the following code. How can I solve this problem?

while (reader.ReadInt32() != 101010256)
{
   reader.BaseStream.Seek(-5, SeekOrigin.Current);  // this line causes error
}...

Also, where do I need to put this code and where do I specify it as the destination directory?

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(@"audio.rar", FileMode.Open, FileAccess.ReadWrite))
{
    UnZipper unzip = new UnZipper(fileStream);                               
    foreach (string filename in unzip.FileNamesInZip())
    {
       string FileName = filename;
    }
}
+4
source share
3 answers

Silverlight SharpZipLib. SharpZipLib.WindowsPhone7.dll ( WP8 silverlight).

    private void Unzip()
    {
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            ZipEntry entry;
            int size;
            byte[] data = new byte[2048];

            using (ZipInputStream zip = new ZipInputStream(store.OpenFile("YourZipFile.zip", FileMode.Open)))
            {
                // retrieve each file/folder
                while ((entry = zip.GetNextEntry()) != null)
                {
                    if (!entry.IsFile)
                        continue;

                    // if file name is music/rock/new.mp3, we need only new.mp3
                    // also you must make sure file name doesn't have unsupported chars like /,\,etc.
                    int index = entry.Name.LastIndexOf('/');

                    string name = entry.Name.Substring(index + 1);

                    // create file in isolated storage
                    using (var writer = store.OpenFile(name, FileMode.Create))
                    {
                        while (true)
                        {
                            size = zip.Read(data, 0, data.Length);
                            if (size > 0)
                                writer.Write(data, 0, size);
                            else
                                break;
                        }
                    }
                }
            }
        }

    }
0

ZIP WP8, ZipLib, @http://slsharpziplib.codeplex.com/ DotNetZip ZipLib . . , , , .

     ZipFile zip = ZipFile.Read(ZipFileToUnzip);

 foreach (ZipEntry ent in zip)
 {
    ent.Extract(DirectoryWhereToUnizp, ExtractExistingFileAction.OverwriteSilently);
 }
0

. , , , zip . , .

I just read the sample.zip file. From the folder of your application.

private async Task UnZipFile()
    {
        var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
        using (var fileStream = Application.GetResourceStream(new Uri("sample.zip", UriKind.Relative)).Stream)
        {
            var unzip = new UnZipper(fileStream);
            foreach (string filename in unzip.FileNamesInZip)
            {
                if (!string.IsNullOrEmpty(filename))
                {
                    if (filename.Any(m => m.Equals('/')))
                    {
                        myIsolatedStorage.CreateDirectory(filename.Substring(0, filename.LastIndexOfAny(new char[] { '/' })));
                    }

                    //save file entry to storage
                    using (var streamWriter =
                        new StreamWriter(new IsolatedStorageFileStream(filename,
                            FileMode.Create,
                            FileAccess.ReadWrite,
                            myIsolatedStorage)))
                    {
                        streamWriter.Write(unzip.GetFileStream(filename));
                    }
                }
            }
        }
    }

cheers :)

0
source

Source: https://habr.com/ru/post/1535204/


All Articles