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. Oracle Grid 11.2.0.4 安装是出现“[INS-41112] Specified network interface doesnt maintain connectivity across cluster”错误

    最新文章:Virson's Blog 安装Oracle 11.2.0.4 的RAC,在Grid 安装时报错: [INS-41112]Specified network interface doesnt ...

  2. SSH远程连接Linux配置

    CentOS:   开启远程连接服务:service sshd start 添加到系统启动项:chkconfig sshd on 客户端工具:windows下连接工具putty   ========= ...

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

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

  4. adb shell dumpsys 命令

    Android开发中,常常可以用adb shell dumpsys这条命令来dump出系统运行时的状态信息,例如可以这样来察看某个应用的内存使用信息 adb shell dumpsys meminfo ...

  5. 生产环境使用nginx做负载均衡配置的五种策略

    nginx的upstream目前支持5种方式的分配1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight指定轮询几率,weight和访 ...

  6. Spring task定时任务执行一段时间后莫名其妙停止的问题

    前因: 我写了一个小项目,主要功能是用Spring task定时任务每天定时给用户发送邮件.执行了几个月一直没有问题,前几天,莫名其妙的突然不再发送邮件了. 只好花费一些时间来查看到底是什么原因造成的 ...

  7. 【代码审计】XYHCMS V3.5文件上传漏洞分析

      0x00 环境准备 XYHCMS官网:http://www.xyhcms.com/ 网站源码版本:XYHCMS V3.5(2017-12-04 更新) 程序源码下载:http://www.xyhc ...

  8. JS正则校验

    /** 用途:检查输入字符串是否为空或者全部都是空格 输入:str:字符串 返回: 如果全是空返回true,否则返回false */ function isNull(str) { if (str == ...

  9. Redis----整理

    --------------------------------------------------------------------redis--------------------------- ...

  10. java8 集合对象间的处理

    eg1:List<CarVo> carVoList = carService.getList(carVo); List<String> listVins = carVoList ...