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. EventBus vs Otto vs Guava--自定义消息总线

    同步发表于http://avenwu.net/ioc/2015/01/29/custom_eventbus Fork on github https://github.com/avenwu/suppo ...

  2. 性能优化系列七:SQL优化

    一.SQL在数据库中的执行过程 二.执行计划 1. ACID 原子性:一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节.事务在执行过程中发生错误,会 ...

  3. yum常用命令大全

    yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性 ...

  4. Python 函数 (关键字参数)

    关键字参数 可变参数允许你传入0个或者任意个参数,这些可变参数在函数调用时会自动组装成一个tuple,而关键字参数允许你传入0个或者任意个含参数名的参数,这些参数在函数内部自动组装成为一个dict d ...

  5. [Tensorflow] Object Detection API - prepare your training data

    From: TensorFlow Object Detection API This chapter help you to train your own model to identify obje ...

  6. aws平台中为ec2实例添加双网卡

    亚马逊平台创建ec2实例时默认创建一个网卡primary网卡,主网卡是不能分离的. 在ec2服务控制台为ec2附加网卡. 平台资料中为正在使用的ec2添加网卡用到的操作系统是Amazon Linux ...

  7. 【代码审计】XYHCMS V3.5URL重定向漏洞分析

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

  8. 解决UEFI启动模式下无法使用U盘启动WIN7安装界面

    问题场景 现在很多人都习惯使用U盘进行安装系统,主要是快捷方便.本文主要是讲解一下U盘在UEFI模式下无法启动Windows7安装界面的问题,可能很多人会说使用PE系统进行安装,但是因为我的主板只有独 ...

  9. WCF中序列化(XML\JSON\Dt)

    序列化 是将对象转换为容易传输的格式的过程.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象.反之,反序列化根据流重新构造对象. 序列化描述了持久化 ...

  10. ModelAttribue注解的使用

    Spring中有很多注解,如RequestParam,PathVarible,SesstionAttribute,这些在开发是多尝试一下,可能用得到,ModelAttribute用的还挺多,可以以此为 ...