zoukankan      html  css  js  c++  java
  • 利用ICSharpCode.SharpZipLib.Zip进行文件压缩

    官网http://www.icsharpcode.net/

    支持文件和字符压缩。

    创建全新的压缩包

    第一步,创建压缩包

    using ICSharpCode.SharpZipLib.Zip;
    ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
    参数:strZipPath,提供压缩后的文件包全路径,即地址和文件名。如,D:	mp1.rar
    第二步,向压缩包中添加压缩包的文件名
    ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(name);
    entry.DateTime = System.DateTime.Now; zipOutputStream.PutNextEntry(entry);
    如01.rar压缩包中包含A.txt,B.txt,C.txt三个文件,那么参数name则对应为A.txt,B.txt,C.txt。
    第三步,向压缩包中的entry写入文件
    System.IO.FileStream fs = System.IO.File.OpenRead(strFile);
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    zipOutputStream.Write(buffer, 0, buffer.Length);
    fs.Close();
    fs.Dispose();

    参数:strFile,待压缩的文件全路径。

    第二步、第三步,可以循环,向压缩包中添加更多的文件。

    向已有的压缩包中添加新文件

    ICSharpCode.SharpZipLib.Zip.ZipFile z = new ICSharpCode.SharpZipLib.Zip.ZipFile();

    唯一区别是使用ZipFile代替ZipOutputStream,打开已有的压缩文件。

    每天学习,进步一点点。

  • 相关阅读:
    CodeForces
    处女座的测验(一)(素数筛+思维)
    Codeforces-D-Diverse Garland(思维)
    linux中open函数使用
    linux管道通信
    linux中memset的正确用法
    在linux中read、write函数
    Ubuntu+Win7+Samba实现文件共享
    【转】教你如何实现linux和W…
    《转》我的ARM学习经历
  • 原文地址:https://www.cnblogs.com/lucika/p/3884210.html
Copyright © 2011-2022 走看看