zip压缩类
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压缩类的更多相关文章
- php ZIP压缩类实例分享
php ZIP压缩类实例分享 <?php $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt ...
- C#zip压缩类
改造了网上的代码,可以压缩文件夹.指定文件列表(文件和文件夹的混合列表,需要指定子文件夹下需要压缩的文件),注释很详细不做解释 public class ZipHelper { /// <sum ...
- 一个zip压缩类,欢迎吐槽
package com.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import j ...
- C#实现Zip压缩解压实例
原文地址:https://www.cnblogs.com/GoCircle/p/6544678.html 本文只列举一个压缩帮助类,使用的是有要添加一个dll引用ICSharpCode.SharpZi ...
- Java操作zip压缩和解压缩文件工具类
需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...
- AntZipUtils【基于Ant的Zip压缩解压缩工具类】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 Android 压缩解压zip文件一般分为两种方式: 基于JDK的Zip压缩工具类 该版本存在问题:压缩时如果目录或文件名含有中文, ...
- 文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.
FileOperationUtils.java package com.xnl.utils; import java.io.BufferedInputStream; import java.io.Bu ...
- php压缩zip文件类
使用文件压缩类, 注意传的路径是相对路径.如果传绝对路径就把addFile里面的第二个参数去掉/ $zip = new ZipFolder(); $zipFile = './autoloadClass ...
- java将文件打包成ZIP压缩文件的工具类实例
package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
随机推荐
- 一个相对通用的JSON响应结构,其中包含两部分:元数据与返回值
定义一个相对通用的JSON响应结构,其中包含两部分:元数据与返回值,其中,元数据表示操作是否成功与返回值消息等,返回值对应服务端方法所返回的数据. public class Response { pr ...
- 关于Unity的两种调试方法
Unity的两种调试方法 1.Debug.Log()输出语句调试,平时经常用这个 2.把MonoDevelop和Unity进行连接后断点调试 先把编辑器选择为MonoDevelop,Edit----& ...
- SharePreferences
SharePreferences是一种轻量级的数据存储方式,它是以key-value的形式保存在 data/data/<packagename>/shared_prefs 下的xml文件中 ...
- [JS] ECMAScript 6 - Set & Map : compare with c#
Ref: Set 和 Map 数据结构 Day 0 - 1所学
- 06建造者模式Builder
一.什么是建造者模式 Builder模式也叫建造者模式或者生成器模式, 是由GoF提出的23种设计模式中的一种. Builder模式是一种对象创建型模式之一,用来 隐藏复合对象的创建过程,它把复合对象 ...
- Tomcat启动报Error listenerStart错误 Context [] startup failed due to previous errors
本文转载自xpenxpen 今天启动Tomcat启动不了,报以下错: org.apache.catalina.core.StandardContext startInternal SEVERE: Er ...
- C# Hashtable
哈希表(Hashtable) 在.NET Framework中,Hashtable 是 System.Collections 命名空间提供的一个容器,用于处理和表现类似 key-value 的键值对, ...
- C#5种方式生成缩略图
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- TOP100summit:【分享实录-WalmartLabs】利用开源大数据技术构建WMX广告效益分析平台
本篇文章内容来自2016年TOP100summitWalmartLabs实验室广告平台首席工程师.架构师粟迪夫的案例分享. 编辑:Cynthia 粟迪夫:WalmartLabs实验室广告平台首席工程师 ...
- Echarts 的Formatter的回调函数
option = { tooltip: { trigger: 'axis', formatter: function (params,ticket,callback) { let res = para ...