New Posts New Posts RSS Feed: Compressing Cache in Local Storage
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Compressing Cache in Local Storage

 Post Reply Post Reply
Author
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Topic: Compressing Cache in Local Storage
    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, 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.
Back to Top
dswersky View Drop Down
Newbie
Newbie
Avatar

Joined: 23-Mar-2011
Location: Cleveland, OH
Posts: 1
Post Options Post Options   Quote dswersky Quote  Post ReplyReply Direct Link To This Post 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();

}

}

}

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down