2011-04-08 23 views
0

私のアプリケーションは、大量のキャッシュデータをローカルストレージに格納して、パフォーマンスと接続を切断します。 SharpZipLibを使用して作成されたキャッシュファイルを圧縮しようとしましたが、いくつか問題があります。プログラムでZIPファイルを作成する

ファイルを作成できますが、無効です。 Windowsの組み込みのzipシステムと7-zipは両方ともファイルが無効であることを示しています。 SharpZipLibを使用してプログラムでプログラムを開こうとすると、「誤った中央ディレクトリの署名」という例外が発生します。問題の一部は、私がMemoryStreamから直接zipファイルを作成しているので、 "ルート"ディレクトリがないということです。 SharpZipLibを使用してプログラムで作成する方法がわかりません。

以下のEntityManagerは、DevForceで生成された「datacontext」です。キャッシングのためにディスクにシリアライズする目的で、その内容をストリームに保存することができます。

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();  

       } 
      } 
     } 

答えて

0

メモリから直接zipファイルを作成されていない問題:

は、ここに私のコードです。 SharpZipLibは、パスを決定するためにZipEntryコンストラクタのパラメータを使用し、そのパスにサブディレクトリがあるかどうかは気にしません。

using (ZipOutputStream zipStreamOut = new ZipOutputStream(outputstream)) 
{ 
    zipStreamOut.PutNextEntry(new ZipEntry("arbitrary.ext")); 
    zipstreamOut.Write(mybytearraydata, 0, mybytearraydata.Length); 
    zipStreamOut.Finish(); 
    //Line below needed if outputstream is a MemoryStream and you are 
    //passing it to a function expecting a stream. 
    outputstream.Position = 0; 

    //DoStuff. Optional; Not necessary if e.g., outputstream is a FileStream. 
} 
-1

outStream.Position = 0;を削除しても機能します。