Print Page | Close Window

Compressing Cache in Local Storage

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2615
Printed Date: 29-Jul-2026 at 8:31pm


Topic: Compressing Cache in Local Storage
Posted By: dswersky
Subject: Compressing Cache in Local Storage
Date Posted: 08-Apr-2011 at 7:09am
My application will be storing large amounts of cached data for performance and disconnected purposes.  I have tried to use SharpZipLib to compress the cache files that are created, but I'm having some difficulty.
 
I can get the file created, but it is invalid.  Windows' built-in zip system and 7-zip both indicate that the file is invalid.  When I attempt to open the file programmatically through SharpZipLib, I get the exception "Wrong Central Directory signature."  I think part of the problem is that I'm creating the zip file directly from a MemoryStream, so there is no "root" directory in the archive.  Not sure how to create one programmatically with SharpZipLib.
 
Here's my code:
 
private void SaveCacheFile(string FileName, EntityManager em)

{

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())

{

using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(FileName, System.IO.FileMode.CreateNew, isf))

{

MemoryStream inStream = new MemoryStream();

MemoryStream outStream = new MemoryStream();

Crc32 crc = new Crc32();

em.CacheStateManager.SaveCacheState(inStream,

false, true);

inStream.Position = 0;

ZipOutputStream zipStream = new ZipOutputStream(outStream);

zipStream.IsStreamOwner =

false;

zipStream.SetLevel(3);

ZipEntry newEntry = new ZipEntry(FileName);

byte[] buffer = new byte[inStream.Length];

inStream.Read(buffer, 0, buffer.Length);

newEntry.DateTime =

DateTime.Now;

newEntry.Size = inStream.Length;

crc.Reset();

crc.Update(buffer);

newEntry.Crc = crc.Value;

zipStream.PutNextEntry(newEntry);

buffer =

null;

outStream.Position = 0;

inStream.Position = 0;

StreamUtils.Copy(inStream, zipStream, new byte[4096]);

zipStream.CloseEntry();

zipStream.Finish();

zipStream.Close();

outStream.Position = 0;

StreamUtils.Copy(outStream, isfs, new byte[4096]);

outStream.Close();

}

}

}




Replies:
Posted By: DenisK
Date Posted: 11-Apr-2011 at 1:16pm
Hi dswersky;

Actually, DevForce already has a built-in class under IdeaBlade.Core.Ionic.Zlib.GZipStream to provide EntityCacheState compression capabilities.

Using this page,  http://drc.ideablade.com/xwiki/bin/view/Documentation/code-sample-save-restore-chained-streams - http://drc.ideablade.com/xwiki/bin/view/Documentation/code-sample-save-restore-chained-streams , as a guidance. I was able to modify the functions just a little bit so it uses IsolatedStorageFile for Silverlight.


      public void SaveCompressedEncryptedFile(EntityManager em, byte[] key, string fileName) {
        var provider = new AesManaged { Key = key, IV = key };
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) {
          using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, 
            System.IO.FileMode.Create, isf)) {
            using (CryptoStream cs = new CryptoStream(isfs, provider.CreateEncryptor(), CryptoStreamMode.Write)) {
              using (GZipStream gz = new GZipStream(cs, CompressionMode.Compress)) {
                em.CacheStateManager.SaveCacheState(gz);
              }
            }
          }
        }
      }



      public void RestoreCompressedEncryptedFile(EntityManager em, byte[] key, string fileName) {
        var provider = new AesManaged { Key = key, IV = key };
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) {
          using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName,
            System.IO.FileMode.Open, isf)) {
            using (CryptoStream cs = new CryptoStream(isfs, provider.CreateDecryptor(), CryptoStreamMode.Read)) {
              using (GZipStream gz = new GZipStream(cs, CompressionMode.Decompress)) {
                em.CacheStateManager.RestoreCacheState(gz, RestoreStrategy.Normal);
              }
            }
          }
        }
      }


Hope this helps.



Print Page | Close Window