首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >压缩/解压缩文件夹和文件

压缩/解压缩文件夹和文件
EN

Stack Overflow用户
提问于 2008-08-01 17:13:09
回答 9查看 5.1K关注 0票数 60

有谁知道在C#中快速压缩或解压缩文件和文件夹的好方法吗?处理大文件可能是必要的。

EN

回答 9

Stack Overflow用户

发布于 2008-08-05 12:04:41

这里有两个方法可以压缩和解压一个字节流,你可以从你的文件对象中获得。

代码语言:javascript
复制
public static byte[] Compress(byte[] data)
{
    MemoryStream output = new MemoryStream();

    GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true);
    gzip.Write(data, 0, data.Length);
    gzip.Close();

    return output.ToArray();
}

public static byte[] Decompress(byte[] data)
{
    MemoryStream input = new MemoryStream();
    input.Write(data, 0, data.Length);
    input.Position = 0;

    GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);

    MemoryStream output = new MemoryStream();

    byte[] buff = new byte[64];
    int read = -1;

    read = gzip.Read(buff, 0, buff.Length);

    while (read > 0)
    {
        output.Write(buff, 0, read);
        read = gzip.Read(buff, 0, buff.Length);
    }

    gzip.Close();

    return output.ToArray();
}
票数 27
EN

Stack Overflow用户

发布于 2008-08-01 17:30:56

我一直在使用SharpZip库。

Here's a link

票数 22
EN

Stack Overflow用户

发布于 2008-08-01 17:28:24

在Java1.1中,唯一可用的方法是进入.Net库。

Using the Zip Classes in the J# Class Libraries to Compress Files and Data with C#

不确定这在最近的版本中是否发生了变化。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/145

复制
相关文章

相似问题

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