1

ファイルが作成され、サイズは問題ないようですが、ダブルクリックすると、形式が間違っているか、ファイルが破損していると表示されます。

これは私が使用しているコードです

public MemoryStream CompressFiles(Dictionary<string, MemoryStream> filesToBeCompressed)
{
    var output = new MemoryStream();
    using (var zip = new ZipFile())
    {
        foreach (var entry in filesToBeCompressed)
        {
            entry.Value.Seek(0, SeekOrigin.Begin); // <-- must do this after writing the stream (I've read this in a blog
            zip.AddEntry(entry.Key.Substring(entry.Key.LastIndexOf('/') + 1, entry.Key.Length - entry.Key.LastIndexOf('/') - 1), entry.Value);
            zip.Save(output);
        }
    }
    return output;
}

次に、呼び出し元のメソッドで

SaveStreamToFile(documentCompressedName,getDocument());

getDocument()は内部でCompressを呼び出します

そしてついにその方法

private static void SaveStreamToFile(string fileFullPath, Stream stream)
{
    if (stream.Length == 0) return;

    // Create a FileStream object to write a stream to a file
    using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
    {
        // Fill the bytes[] array with the stream data
        var bytesInStream = new byte[stream.Length];
        stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

        // Use FileStream object to write to the specified file
        fileStream.Write(bytesInStream, 0, bytesInStream.Length);
    }
}

何か案は?前もって感謝します!ギレルモ。

4

2 に答える 2

3

問題はあなたの機能にあると思いますSaveStreamToFile。アーカイブをディスクに書き込む前に、ストリームの位置を先頭に設定する必要があります。

private static void SaveStreamToFile(string fileFullPath, Stream stream)
{
  if (stream.Length == 0) return;

  // Set the position within the stream to the beginning of the stream
  stream.Seek(0, SeekOrigin.Begin);      

  // Create a FileStream object to write a stream to a file
  using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
  {
    // Fill the bytes[] array with the stream data
    var bytesInStream = new byte[stream.Length];
    stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

    // Use FileStream object to write to the specified file
    fileStream.Write(bytesInStream, 0, bytesInStream.Length);
  }
}

お役に立てれば。

于 2011-10-05T19:34:46.387 に答える
1

コードスニペットから、ここでの私の推測では、MemoryStreamPositionは、に渡すときにストリームの最後にあり、ストリームSaveStreamToFileの最初に位置を戻すことは決してないため、stream.Read実際にはバイトをまったく読み取っていません。出力zipファイルを16進エディターで開くと、おそらくゼロでいっぱいになっていることがわかります。

ここにはいくつかのオプションがありますが、私の提案は次のことを試してみることです。

private static void SaveStreamToFile(string fileFullPath, Stream stream)
{
    if (stream.Length == 0) return;

    // Create a FileStream object to write a stream to a file
    using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
    {
        // Use FileStream object to write to the specified file
        fileStream.Write(stream.GetBuffer(), 0, stream.Length);
    }
}

このアプローチは、の内部メモリバッファのコピーを取ることを回避しますMemoryStream。zipファイルの大きさはわかりませんが、メモリ使用量の観点からは問題にならないかもしれませんが、zipファイルをメモリに2回保存します。1回は元の配列MemoryStreamに、もう1回は元のbytesInStream配列に保存する必要はないようです。

于 2011-10-05T19:35:31.607 に答える