使用vb.net和System.IO.Compression(.Net 4.5)压缩文件

时间:2014-03-26 20:29:24

标签: vb.net zip

鉴于这两个文件:c:\ folder1 \ test.txt和c:\ folder2 \ test.txt

在VB.NET上使用.Net 4.5 System.IO.Compression创建zip文件(c:\ test.zip)并添加2个文件并包含目录结构的最佳方法是什么?

感谢。

1 个答案:

答案 0 :(得分:0)

答案位于网站

.NET GZipStream compress and decompress

      public static bool Test()
        {
        string sample = "This is a compression test of microsoft .net gzip compression method and            decompression methods";

        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

        byte[] data = encoding.GetBytes(sample);

        // Compress.
        GZipStream hgs;
        byte[] cmpData;

        using(MemoryStream cmpStream = new MemoryStream())
        using(hgs = new GZipStream(cmpStream, CompressionMode.Compress))
        {
            hgs.Write(data, 0, data.Length);

            cmpData = cmpStream.ToArray();
        }

        using(MemoryStream decomStream = new MemoryStream(cmpData))
        using(hgs = new GZipStream(decomStream, CompressionMode.Decompress))
        {
            hgs.Read(data, 0, data.Length);
        }

        string sampleOut = encoding.GetString(data);

        bool result = String.Equals(sample, sampleOut);
        return result;
    }