using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO; using ICSharpCode.SharpZipLib.Zip; namespace Test
{
public class ZIP
{
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileName">待压缩文件完整路径</param>
/// <param name="srcName">压缩后文件完整路径</param>
public static string ZipFile(string srcName, string zipName)
{
srcName = new Regex("[\\/]+").Replace(srcName, "/");
zipName = new Regex("[\\/]+").Replace(zipName, "/");
string errorMsg = "";
try
{
if (!File.Exists(srcName))
{
throw new Exception("指定的压缩文件不存在!");
}
ZipOutputStream zos = new ZipOutputStream(File.Create(zipName));
AddZipEntry(srcName, srcName.Substring(, srcName.LastIndexOf("/")), zos, out zos);
zos.Close();
}
catch (Exception ex)
{
errorMsg = ex.Message;
} return errorMsg;
} /// <summary>
/// 压缩目录
/// </summary>
/// <param name="directory">待压缩目录</param>
/// <param name="zipName">压缩后文件完整路径</param>
public static string ZipDirectory(string directory, string zipName)
{
directory = new Regex("[\\/]+").Replace(directory, "/");
zipName = new Regex("[\\/]+").Replace(zipName, "/");
string errorMsg = "";
try
{
if (!Directory.Exists(directory))
{
throw new Exception("指定的压缩目录不存在!");
}
ZipOutputStream zos = new ZipOutputStream(File.Create(zipName));
DirectoryInfo di = new DirectoryInfo(directory);
foreach (DirectoryInfo item in di.GetDirectories())
{
AddZipEntry(item.FullName, directory, zos, out zos);
}
foreach (FileInfo item in di.GetFiles())
{
AddZipEntry(item.FullName, directory, zos, out zos);
}
zos.Close();
}
catch (Exception ex)
{
errorMsg = ex.Message;
} return errorMsg;
} /// <summary>
/// 添加压缩项
/// </summary>
/// <param name="name">目录/文件完整路径</param>
/// <param name="zos1"></param>
/// <param name="zos2"></param>
private static void AddZipEntry(string name, string rootPath, ZipOutputStream zos1, out ZipOutputStream zos2)
{
ZipEntry ze = null; //若待压缩项是个目录
if (Directory.Exists(name))
{
DirectoryInfo di = new DirectoryInfo(name);
if (di.GetDirectories().Length == ) //添加空目录
{
ze = new ZipEntry(GetFilePath(name, rootPath) + "/");
zos1.PutNextEntry(ze);
}
foreach (DirectoryInfo item in di.GetDirectories()) //添加子目录
{
ze = new ZipEntry(GetFilePath(item.FullName, rootPath) + "/");
zos1.PutNextEntry(ze);
AddZipEntry(item.FullName, rootPath, zos1, out zos1); //递归添加目录
}
foreach (FileInfo item in di.GetFiles())
{
AddZipEntry(item.FullName, rootPath, zos1, out zos2); //递归添加文件
}
} //若待压缩项是个文件
if (File.Exists(name))
{
zos1.SetLevel();
FileStream fs = File.OpenRead(name);
int index = ;
byte[] bs = new byte[];
ze = new ZipEntry(GetFilePath(name, rootPath));
zos1.PutNextEntry(ze);
while ((index = fs.Read(bs, , )) != )
{
zos1.Write(bs, , index);
}
fs.Close();
} zos2 = zos1;
} /// <summary>
/// 解压缩文件
/// </summary>
/// <param name="zipFile">待解压文件</param>
/// <param name="dePath">解压缩至dePath目录下</param>
public static string UnZipFile(string zipFile, string dePath)
{
zipFile = new Regex("[\\/]+").Replace(zipFile, "/");
dePath = new Regex("[\\/]+").Replace(dePath, "/");
string errorMsg = "";
try
{
if (!File.Exists(zipFile))
{
throw new Exception("待解压文件不存在!");
} if (!Directory.Exists(dePath))
{
Directory.CreateDirectory(dePath);
} ZipInputStream zis = new ZipInputStream(File.OpenRead(zipFile));
ZipEntry ze = null;
string directoryPath = ""; //目录完整路径
string filePath = ""; //文件完整路径
Regex regex = new Regex("[/\\\\]+");
FileStream fs = null;
byte[] bs = new byte[];
int index = ; while ((ze = zis.GetNextEntry()) != null)
{
if (ze.IsDirectory) //如果是目录
{
directoryPath = dePath + "/" + ze.Name.Substring(, ze.Name.LastIndexOf("/"));
directoryPath = regex.Replace(directoryPath, "/");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}
else // 否则为文件
{
if (ze.Crc != 00000000L) ////此ZipEntry不是标记文件
{
filePath = dePath + "/" + ze.Name;
filePath = regex.Replace(filePath, "/"); directoryPath = IOUtils.GetFilePath(filePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
fs = File.Open(filePath, FileMode.Create, FileAccess.Write); while ((index = zis.Read(bs, , )) != )
{
fs.Write(bs, , index);
}
fs.Close();
}
}
} zis.Close();
}
catch (Exception ex)
{
errorMsg = ex.Message;
} return errorMsg;
} /// <summary>
/// 获取文件相对路径
/// </summary>
private static string GetFilePath(string filePath, string rootPath)
{
rootPath = new Regex("/+").Replace(rootPath + "/", "/");
filePath = new Regex("/+").Replace(filePath, "/"); return filePath.Replace(rootPath, "");
}
}
}

zip压缩类的更多相关文章

  1. php ZIP压缩类实例分享

    php ZIP压缩类实例分享 <?php $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt ...

  2. C#zip压缩类

    改造了网上的代码,可以压缩文件夹.指定文件列表(文件和文件夹的混合列表,需要指定子文件夹下需要压缩的文件),注释很详细不做解释 public class ZipHelper { /// <sum ...

  3. 一个zip压缩类,欢迎吐槽

    package com.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import j ...

  4. C#实现Zip压缩解压实例

    原文地址:https://www.cnblogs.com/GoCircle/p/6544678.html 本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZi ...

  5. Java操作zip压缩和解压缩文件工具类

    需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...

  6. AntZipUtils【基于Ant的Zip压缩解压缩工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 Android 压缩解压zip文件一般分为两种方式: 基于JDK的Zip压缩工具类 该版本存在问题:压缩时如果目录或文件名含有中文, ...

  7. 文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.

    FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.Bu ...

  8. php压缩zip文件类

    使用文件压缩类, 注意传的路径是相对路径.如果传绝对路径就把addFile里面的第二个参数去掉/ $zip = new ZipFolder(); $zipFile = './autoloadClass ...

  9. java将文件打包成ZIP压缩文件的工具类实例

    package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

随机推荐

  1. Ubuntu命令行

    1.打开图形界面的终端 打开:Ctrl+Alt+T 退出:Ctrl+D 2.打开DOS界面的终端 打开:Ctrl+Alt+F1(F1~F6) 退出:Ctrl+Alt+F7 3.用户 ubuntu初始r ...

  2. swoole 定时器

    timer.php <?php //创建websocket服务器对象,监听0.0.0.0:9502端口 $ws = ); swoole_timer_tick(, function ($timer ...

  3. angular 4 和django 1.11.1 前后端交互 总结

    首先 angular4 和django 1.11.1交互 有跨域问题 所以先关闭cors 和csrf验证 一.解决跨域问题 cors github django-cors-headers 1)安装co ...

  4. 使用Postmark测试后端存储性能

    Postmark用于对进行频繁,大量存取小文件的存储系统的存储性能测试.原理:构建一个测试文件池,通过文件最大,最小大小,数量等参数进行配置,然后进行事务的初始化,对每一个事务中读取/附加,创建/删除 ...

  5. Office Web Apps Server

    Office Web Apps Server Office Web Apps Server 是一款 Office 服务器产品,可提供针对 Office 文件的基于浏览器的文件查看和编辑服务.Offic ...

  6. 剖析Elasticsearch集群系列之二:分布式的三个C、translog和Lucene段

    转载:http://www.infoq.com/cn/articles/anatomy-of-an-elasticsearch-cluster-part02 共识——裂脑问题及法定票数的重要性 共识是 ...

  7. [转]获取app的内部储存路径

    首先内部存储路径为/data/data/youPackageName/,下面讲解的各路径都是基于你自己的应用的内部存储路径下.所有内部存储中保存的文件在用户卸载应用的时候会被删除. 一. files1 ...

  8. xss攻击问题以及如何防范

    当用户提交评论的时候,比如如下评论内容 111 <scripy>alert(111);</scripy> 这样当现实评论的时候会先弹出111弹框,再显示评论.这就是xss攻击. ...

  9. 使用Python往手机发送短信(基于twilio模块)

    官网是https://www.twilio.com twilio的一句话介绍——提供SDK帮你连接世界上所有人,你可以很方便的调用他们提供的接口来给指定手机发短信,打电话. 首先在twilio的官网注 ...

  10. D - Pagodas

    n pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai Mountain, l ...