Search code examples
windows-phone-7zipunzip

How to speed up the unzip action on windows phone 7?


When I used the SharpZipLib to unzip a zip file which has 5000 files on the windows phone 7. It took more than 5 minutes to finish it. Here is the code:

using (StreamReader httpwebStreamReader = new StreamReader(ea.Result))
            {
                //open isolated storage to save files
                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (ZipInputStream s = new ZipInputStream(httpwebStreamReader.BaseStream))
                    {
                        //s.Password = "123456";//if archive is encrypted
                        ZipEntry theEntry;
                        while ((theEntry = s.GetNextEntry()) != null)
                        {
                            string directoryName = Path.GetDirectoryName(theEntry.Name);
                            string fileName = Path.GetFileName(theEntry.Name);

                            // create directory
                            if (directoryName.Length > 0)
                            {
                                isoStore.CreateDirectory(directoryName);
                            }

                            if (fileName != String.Empty)
                            {
                                //save file to isolated storage
                                using (BinaryWriter streamWriter =
                                        new BinaryWriter(new IsolatedStorageFileStream(theEntry.Name,
                                            FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, isoStore)))
                                {

                                    int size = 2048;
                                    byte[] data = new byte[2048];
                                    while (true)
                                    {
                                        size = s.Read(data, 0, data.Length);
                                        if (size > 0)
                                        {
                                            streamWriter.Write(data, 0, size);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

Why it's so slow? How can I speed up the unzip action? Anyone knows?


Solution

  • I think you need to increase your buffer size. Change the lines

    int size = 2048;
    byte[] data = new byte[2048];
    

    And change the 2048 to something like 32768 (32*1024).

    A 2KB block size is making a lot of individual writes to the flash storage. In my experience that's a somewhat slow thing and can vary from device to device. A 32KB block size should do 16 times fewer but I don't know if that will result in a direct 16x speedup. I'm interested to hear back.