0

私のアプリケーションは、パフォーマンスと切断の目的で、キャッシュされた大量のデータをローカル ストレージに保存します。SharpZipLib を使用して、作成されたキャッシュ ファイルを圧縮しようとしましたが、問題が発生しています。

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

以下の EntityManager は、IdeaBlade DevForce によって生成された「データ コンテキスト」です。キャッシュのためにディスクにシリアル化する目的で、その内容をストリームに保存できます。

これが私のコードです:

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

                }
            }
        }
4

2 に答える 2

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.
}
于 2011-04-15T15:37:51.963 に答える
-2

削除するoutStream.Position = 0;と動作します。

于 2011-11-01T23:02:55.243 に答える