2

I have a scenario where by I want to zip an email attachment using SharpZipLib. Then the end user will open the attachment and will unzip the attached file.

Will the file originally zipped file using SharpZipLib be easily unzipped by other programs for my end user?

4

1 に答える 1

4

SharpZipLib の使用方法によって異なります。このライブラリを使用してデータを圧縮する方法は複数あります。

ほとんどすべての zip 対応アプリケーションで開くことができる zip ファイルを作成する方法の例を次に示します。

private static byte[] CreateZip(byte[] fileBytes, string fileName)
{
    using (var memoryStream = new MemoryStream())
    using (var zipStream = new ZipOutputStream(memoryStream))
    {
        var crc = new Crc32();
        crc.Reset();
        crc.Update(fileBytes);

        var zipEntry =
            new ZipEntry(fileName)
            {
                Crc = crc.Value,
                DateTime = DateTime.Now,
                Size = fileBytes.Length
            };
        zipStream.PutNextEntry(zipEntry);
        zipStream.Write(fileBytes, 0, fileBytes.Length);
        zipStream.Finish();
        zipStream.Close();
        return memoryStream.ToArray();
    }
}

使用法:

var fileBytes = File.ReadAllBytes(@"C:/1.xml");

var zipBytes = CreateZip(fileBytes, "MyFile.xml");

File.WriteAllBytes(@"C:/2.zip", zipBytes);

この CreateZip メソッドは、メモリ内に既にバイトがあり、それらを圧縮してディスクに保存せずに送信したい場合に最適化されています。

于 2011-06-03T01:30:31.653 に答える