首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SharpZipLib -零字节

SharpZipLib -零字节
EN

Stack Overflow用户
提问于 2014-09-03 15:16:47
回答 1查看 639关注 0票数 1

我正在尝试实现SharpZipLib

因此,我从他们的样本中复制了以下片段:

代码语言:javascript
复制
// 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;
}

我复制、粘贴了这个函数,并这样称呼它:

代码语言:javascript
复制
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));
}

myzip.zip是成功创建的,但是里面的file.jpg没有任何字节。

有什么想法吗?

非常感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-21 07:51:38

在写入输入文件数据之后,有必要“倒带”输入MemoryStream

代码语言:javascript
复制
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);
    }
}

备选(略为简化的)实现版本:

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25647997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档