简体   繁体   English

SharpZipLib - 零字节

[英]SharpZipLib - zero bytes

I am trying to implement SharpZipLib我正在尝试实施SharpZipLib

So I copied the below snippet from their samples:所以我从他们的样本中复制了以下片段:

// Compresses the supplied memory stream, naming it as zipEntryName, into a zip,
// which is returned as a memory stream or a byte array.
//
public MemoryStream CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName) 
{
    MemoryStream outputMemStream = new MemoryStream();
    ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);

    zipStream.SetLevel(3); //0-9, 9 being the highest level of compression

    ZipEntry newEntry = new ZipEntry(zipEntryName);
    newEntry.DateTime = DateTime.Now;

    zipStream.PutNextEntry(newEntry);

    StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
    zipStream.CloseEntry();

    zipStream.IsStreamOwner = false;    // False stops the Close also Closing the underlying stream.
    zipStream.Close();          // Must finish the ZipOutputStream before using outputMemStream.

    outputMemStream.Position = 0;
    return outputMemStream;

    // Alternative outputs:
    // ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
    byte[] byteArrayOut = outputMemStream.ToArray();

    // GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
    byte[] byteArrayOut = outputMemStream.GetBuffer();
    long len = outputMemStream.Length;
}

I copy pasted that function and called it this way:我复制粘贴该函数并以这种方式调用它:

using (MemoryStream ms = new MemoryStream())
using (FileStream file = new FileStream(@"c:\file.jpg", FileMode.Open, FileAccess.Read))
{
    byte[] bytes = new byte[file.Length];
    file.Read(bytes, 0, (int)file.Length);
    ms.Write(bytes, 0, (int)file.Length);
    var result = SharpZip.CreateToMemoryStream(ms, "file.jpg");
    result.WriteTo(new FileStream(@"c:\myzip.zip", FileMode.Create, System.IO.FileAccess.Write));
}

The myzip.zip is sucessfully created, but the file.jpg inside has zero bytes. myzip.zip已成功创建,但里面的 file.jpg 有零字节。

Any ideas?有任何想法吗?

Thanks a lot非常感谢

It is necessary to "rewind" the input MemoryStream after writing the input file data:写入输入文件数据后需要“倒带”输入MemoryStream

using (MemoryStream ms = new MemoryStream())
using (FileStream file = File.OpenRead(@"input file path"))
{
    byte[] bytes = new byte[file.Length];
    file.Read(bytes, 0, (int)file.Length);
    ms.Write(bytes, 0, (int)file.Length);
    ms.Position = 0; // "Rewind" the stream to the beginning.
    var result = SharpZip.CreateToMemoryStream(ms, "file.jpg");
    using (var outputStream = File.Create(@"output file path"))
    {
        result.WriteTo(outputStream);
    }
}

Alternative (slightly simplified) version of the implementation:替代(稍微简化)版本的实现:

var bytes = File.ReadAllBytes(@"input file path");
using (MemoryStream ms = new MemoryStream(bytes))
{
    var result = SharpZip.CreateToMemoryStream(ms, "file.jpg");
    using (var outputStream = File.Create(@"output file path"))
    {
        result.WriteTo(outputStream);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM