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. Java知多少(9) import及Java类的搜索路径

    如果你希望使用Java包中的类,就必须先使用import语句导入. import语句与C语言中的 #include 有些类似,语法为:    import package1[.package2…].c ...

  2. CAP原理中的一致性

    CAP原理指的是,这三个要素最多只能同时实现两点,不可能三者兼顾.因此在进行分布式架构设计时,必须做出取舍.而对于分布式数据系统,分区容忍性是基本要求,否则就失去了价值.因此设计分布式数据系统,就是在 ...

  3. Linux磁盘概念及其管理工具fdisk

    Linux磁盘概念及其管理工具fdisk [日期:2016-08-27] 来源:Linux社区  作者:chawan [字体:大 中 小]   引言:冯诺依曼体系中的数据存储器就是我们常说的磁盘或硬盘 ...

  4. Oracle 11g EM删除重建的方法

    虚拟机里的Oracle 11g好长时间没用了,突然打开之后发现EM无法访问了,EM可以重建,于是也不打算查找原因了,直接使大招 OS:Windows Server 2012 Oracle:11g R2 ...

  5. linux 如何快速的查找日志中你所要查找的信息

    在工作中我总会通过日志来查找相关问题,但有时候日志太多有不知道又不知道日志什么时候打印的,这时我们可以通过一下方法来查找: 1.把目录跳到你日志文件存放的地方 2.grep  关键字  *    例如 ...

  6. 【代码审计】大米CMS_V5.5.3 SQL注入漏洞分析

      0x00 环境准备 大米CMS官网:http://www.damicms.com 网站源码版本:大米CMS_V5.5.3试用版(更新时间:2017-04-15) 程序源码下载:http://www ...

  7. 【CF506E】Mr. Kitayuta's Gift dp转有限状态自动机+矩阵乘法

    [CF506E]Mr. Kitayuta's Gift 题意:给你一个字符串s,你需要在s中插入n个字符(小写字母),每个字符可以被插在任意位置.问可以得到多少种本质不同的字符串,使得这个串是回文的. ...

  8. Springmvc的原理和业务处理

    要尽量弄懂这个springmvc的工作原理:DispatcherServle,HandlerMapping,HandlerAdapter和ViewResolver等对象协同工作,完成springmvc ...

  9. 9.25中间件和Django的学过的知识总结

    2018-9-25 12:10:54 参考连接:http://www.cnblogs.com/liwenzhou/p/8761803.html 浏览器访问Django的过程 面试时容易问到   Dja ...

  10. 7.8CSS部分的学习!

    <!DOCTYPE html> <html> <head> <title>CSS元素选择器</title> <style type=& ...