zoukankan      html  css  js  c++  java
  • zip (ICSharpCode.SharpZipLib.dll文件需要下载)

    ZipClass zc=new ZipClass ();
    zc.ZipDir(@"E:1新建文件夹", @"E:1新建文件夹.zip", 1);//压缩
    zc.UnZip(@"E:1新建文件夹.zip",@"E:12222");//解压
    

      cs

     1 class ZipClass
     2 {
     3 public void UnZip(string zipFilePath, string unZipDir)
     4 {
     5 if (zipFilePath == string.Empty)
     6 {
     7 throw new Exception("压缩文件不能为空!");
     8 }
     9 if (!File.Exists(zipFilePath))
    10 {
    11 throw new System.IO.FileNotFoundException("压缩文件不存在!");
    12 }
    13 //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹 
    14 if (unZipDir == string.Empty)
    15 unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
    16 if (!unZipDir.EndsWith("//"))
    17 unZipDir += "//";
    18 if (!Directory.Exists(unZipDir))
    19 Directory.CreateDirectory(unZipDir);
    20 
    21 using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
    22 {
    23 
    24 ZipEntry theEntry;
    25 while ((theEntry = s.GetNextEntry()) != null)
    26 {
    27 string directoryName = Path.GetDirectoryName(theEntry.Name);
    28 string fileName = Path.GetFileName(theEntry.Name);
    29 if (directoryName.Length > 0)
    30 {
    31 Directory.CreateDirectory(unZipDir + directoryName);
    32 }
    33 if (!directoryName.EndsWith("//"))
    34 directoryName += "//";
    35 if (fileName != String.Empty)
    36 {
    37 using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
    38 {
    39 
    40 int size = 2048;
    41 byte[] data = new byte[2048];
    42 while (true)
    43 {
    44 size = s.Read(data, 0, data.Length);
    45 if (size > 0)
    46 {
    47 streamWriter.Write(data, 0, size);
    48 }
    49 else
    50 {
    51 break;
    52 }
    53 }
    54 }
    55 }
    56 }
    57 }
    58 }
    59 
    60  
    61 
    62 public static void ZipDir(string sDir, string sZip, ZipOutputStream s)
    63 {
    64 string[] filenames = Directory.GetFiles(sDir);
    65 string[] dirnames = Directory.GetDirectories(sDir);
    66 Crc32 crc = new Crc32();
    67 if (s == null)
    68 {
    69 s = new ZipOutputStream(File.Create(sZip));
    70 s.SetLevel(6); // 0 - store only to 9 - means best compression
    71 }
    72 
    73 
    74 foreach (string file in filenames)
    75 fileZipInStream(file, s);
    76 
    77 foreach (string dir in dirnames)
    78 ZipDir(dir, "", s);
    79 if (sZip != "")
    80 {
    81 s.Finish();
    82 s.Close();
    83 
    84 FileInfo fInfo = new FileInfo(sZip);
    85 long size = fInfo.Length;
    86 //Log.WriteLogD("----------------" + size.ToString());
    87 if (size < 100) 
    88 File.Delete(sZip);
    89 }
    90 }
    91 
    92 }
    View Code

    ICSharpCode.SharpZipLib.dll下载地址:http://i.cnblogs.com/Files.aspx

    感谢提供此类的朋友

  • 相关阅读:
    内网很安全?错错错!附攻击演示
    Fiddler无所不能——之测试开发攻城狮必备利器
    【橙子独创】【假设前置数据异常法】案列解析
    偶发异常BUG,如何高效精准分析排查定位?
    史上最全提现模块案例分解
    移动端推送测试涉及点
    模拟导入系统通讯录5000+手机号 校验批量数据处理是否正常?
    发散逆向思维之查询类列表测试范围的思考
    PICT工具一键生成正交试验用例
    据说黑白红客大多是出身测试行业,那么前戏如何做好呢?戳下
  • 原文地址:https://www.cnblogs.com/yuanjiehot/p/4320976.html
Copyright © 2011-2022 走看看