Partilhar via


Using Compression on Windows Phone 7.5

As I’m sure many of you know who have done development on Windows Phone, there isn’t built in support for compression and decompression. Many folks have pointed to the SharpCompress project on CodePlex, and this is what I ended up using for it as well. The BIG thing that was missing from it though has been any documentation whatsoever for decompressing files on the phone. I had quite a bit of code I had written for a winform application that used the WriteAllToDirectory method to decompress a zipped file on Windows. However, that method doesn’t exist in the assembly for Windows Phone. In addition, you can’t just provide the path to which you can unzip when using the Windows Phone assembly for SharpCompress, you have to give it a stream object.

This ultimately worked, but requires you to do things quite a bit differently from when you use this on the full version of Windows. Here are some tips to get you through this:

1. Enumerate through the Entry collection of the IReader you get from the Reader factory:

//"sr" is a stream reader object from previous code that contains the byte array of the actual zip file

using (var reader = ReaderFactory.Open(sr))

{

while (reader.MoveToNextEntry())

{

   //IsDirectory always returns false in everything I've seen so far

   if (!reader.Entry.IsDirectory)

   {

      //process the entry, described next

   }

   else

   {

       //do not know of a scenario where we can end up here

       Debug.WriteLine(reader.Entry.FilePath);

   }

}

}

2.       Create the directory(s) for the files, then get an IsolatedStorageFileStream instance to which you can write:

//IsDirectory always returns false in everything I've seen so far

if (!reader.Entry.IsDirectory)

{

       IsolatedStorageFileStream theFile = GetLocalFile(reader.Entry.FilePath);

}

 

private IsolatedStorageFileStream GetLocalFile(string fullPath)

{

   IsolatedStorageFileStream fs = null;

 

   try

   {

       //get isolated storage so we can create directories and decompress

       IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

 

       //trim everything from the last index of /, which is the file

       //name, i.e. it will look like folder/folder/folder/filename.txt

       string dirPath = fullPath.Substring(0, fullPath.LastIndexOf("/"));

 

       //what's nice is you can create the full folder path in one call

       if (!storage.DirectoryExists(dirPath))

          storage.CreateDirectory(dirPath);

 

       //now that all the directories exist, create a stream for the

       // file – again, cool because you can just give the full path

       fs = storage.CreateFile(fullPath);

   }

   catch (Exception ex)

   {

       Debug.WriteLine(ex.Message);

   }

 

   return fs;

}

3.       Now that you have a stream, you can write the individual file out use the WriteEntryTo method:

if (!reader.Entry.IsDirectory)

{

   IsolatedStorageFileStream theFile = GetLocalFile(reader.Entry.FilePath);

 

   if (theFile != null)

       reader.WriteEntryTo(theFile);

}

 

Hope this helps someone, the lack of documentation was troublesome so I just had to puzzle through it.

Using Compression on Windows Phone 7.docx

Comments

  • Anonymous
    January 01, 2003
    The comment has been removed