文件压缩与挤压ZIP
/// <summary>
/// Zip压缩与解压缩
/// </summary>
public class ZipHelper
{
/// <summary>
/// 压缩单个文件
/// </summary>
/// <param name="fileToZip">要压缩的文件</param>
/// <param name="zipedFile">压缩后的文件</param>
/// <param name="compressionLevel">压缩等级</param>
/// <param name="blockSize">每次写入大小</param>
public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
{
//如果文件没有找到,则报错
if (!System.IO.File.Exists(fileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
} using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
using (System.IO.FileStream StreamToZip = new System.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + ); ZipEntry ZipEntry = new ZipEntry(fileName); ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(compressionLevel); byte[] buffer = new byte[blockSize]; int sizeRead = ; try
{
do
{
sizeRead = StreamToZip.Read(buffer, , buffer.Length);
ZipStream.Write(buffer, , sizeRead);
}
while (sizeRead > );
}
catch (System.Exception ex)
{
throw ex;
} StreamToZip.Close();
} ZipStream.Finish();
ZipStream.Close();
} ZipFile.Close();
}
} /// <summary>
/// 压缩单个文件
/// </summary>
/// <param name="fileToZip">要进行压缩的文件名</param>
/// <param name="zipedFile">压缩后生成的压缩文件名</param>
public static void ZipFile(string fileToZip, string zipedFile)
{
//如果文件没有找到,则报错
if (!File.Exists(fileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + fileToZip + " 不存在!");
} using (FileStream fs = File.OpenRead(fileToZip))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
fs.Close(); using (FileStream ZipFile = File.Create(zipedFile))
{
using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile))
{
string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + );
ZipEntry ZipEntry = new ZipEntry(fileName);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(); ZipStream.Write(buffer, , buffer.Length);
ZipStream.Finish();
ZipStream.Close();
}
}
}
} /// <summary>
/// 压缩多层目录
/// </summary>
/// <param name="strDirectory">The directory.</param>
/// <param name="zipedFile">The ziped file.</param>
public static void ZipFileDirectory(string strDirectory, string zipedFile)
{
using (System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
{
using (ZipOutputStream s = new ZipOutputStream(ZipFile))
{
ZipSetp(strDirectory, s, "");
}
}
} /// <summary>
/// 递归遍历目录
/// </summary>
/// <param name="strDirectory">The directory.</param>
/// <param name="s">The ZipOutputStream Object.</param>
/// <param name="parentPath">The parent path.</param>
private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
{
if (strDirectory[strDirectory.Length - ] != Path.DirectorySeparatorChar)
{
strDirectory += Path.DirectorySeparatorChar;
}
Crc32 crc = new Crc32(); string[] filenames = Directory.GetFileSystemEntries(strDirectory); foreach (string file in filenames)// 遍历所有的文件和目录
{ if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
string pPath = parentPath;
pPath += file.Substring(file.LastIndexOf("\\") + );
pPath += "\\";
ZipSetp(file, s, pPath);
} else // 否则直接压缩文件
{
//打开压缩文件
using (FileStream fs = File.OpenRead(file))
{ byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length); string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + );
ZipEntry entry = new ZipEntry(fileName); entry.DateTime = DateTime.Now;
entry.Size = fs.Length; fs.Close(); crc.Reset();
crc.Update(buffer); entry.Crc = crc.Value;
s.PutNextEntry(entry); s.Write(buffer, , buffer.Length);
}
}
}
} /// <summary>
/// 解压缩一个 zip 文件。
/// </summary>
/// <param name="zipedFile">The ziped file.</param>
/// <param name="strDirectory">The STR directory.</param>
/// <param name="password">zip 文件的密码。</param>
/// <param name="overWrite">是否覆盖已存在的文件。</param>
public void UnZip(string zipedFile, string strDirectory, string password, bool overWrite)
{ if (strDirectory == "")
strDirectory = Directory.GetCurrentDirectory();
if (!strDirectory.EndsWith("\\"))
strDirectory = strDirectory + "\\"; using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipedFile)))
{
s.Password = password;
ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name; if (pathToZip != "")
directoryName = Path.GetDirectoryName(pathToZip) + "\\"; string fileName = Path.GetFileName(pathToZip); Directory.CreateDirectory(strDirectory + directoryName); if (fileName != "")
{
if ((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
{
int size = ;
byte[] data = new byte[];
while (true)
{
size = s.Read(data, , data.Length); if (size > )
streamWriter.Write(data, , size);
else
break;
}
streamWriter.Close();
}
}
}
} s.Close();
}
} }
引用:ICSharpCode.SharpZipLib.dll
解决文件夹递归压缩BUG
文件压缩与挤压ZIP的更多相关文章
- 文件压缩、解压工具类。文件压缩格式为zip
package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...
- [java]将多个文件压缩成一个zip文件
此文进阶请见:https://www.cnblogs.com/xiandedanteng/p/12155957.html 方法: package zip; import java.io.Buffere ...
- linux下文件压缩与解压操作
对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rap.可 ...
- linux驱动系列之文件压缩解压小节(转)
转至网页:http://www.jb51.net/LINUXjishu/43356.html Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通 ...
- Linux命令 文件压缩及压缩命令
gzip [功能说明] 文件的压缩 #gizp属于GNU软件,总性能不错,是Linux系统首选的压缩工具,tar归档命令的-z参数也是利用gzip/gunzip来解压缩 [语法格式] Gip[选项][ ...
- liunx文件操作 文件压缩
文件备份和压缩命令 在Linux中,常用的文件压缩工具有gzip,bzip2,zip. 'bzip2'是最理想的压缩工具,它提供了最大限度的压缩. 'zip'兼容好,windows也支持. bzip2 ...
- php 文件压缩
1.php文件压缩代码: $zip = new ZipArchive;if($zip->open('aaa.zip',ZipArchive::OVERWRITE)===TRUE){ //aaa. ...
- Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)
1.写入Excel,并加密压缩.不保存文件 String dcxh = String.format("%03d", keyValue); String folderFileName ...
- 下载zip格式文件(压缩Excel文件为zip格式)
Mongodb配置文件参考这一篇:http://www.cnblogs.com/byteworld/p/5913061.html package util; import java.io.Buffer ...
随机推荐
- Matlab 矩阵卷积理解(转载)
转载自:http://blog.csdn.net/andrewseu/article/details/51783181 在图像处理的过程中,经常会看到矩阵卷积的概念,比如说用一个模板去和一张图片进行卷 ...
- ubuntu 以root 运行程序
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- 最小二乘法 java
import java.util.ArrayList; import java.util.Collection; import org.apache.commons.math3.optim.Point ...
- hihoCoder#1055 : 刷油漆 (树形DP+01背包)
题目大意:给一棵带点权的树,现在要从根节点开始选出m个连通的节点,使总权值最大. 题目分析:定义状态dp(u,m)表示在以u为根的子树从根节点开始选出m个点连通的最大总权值,则dp(u,m)=max( ...
- Linux系统编程@终端IO
Linux系统中终端设备种类 终端是一种字符型设备,有多种类型,通常使用tty 来简称各种类型的终端设备.终端特殊设备文件一般有以下几种: 串行端口终端(/dev/ttySn ) ,伪终端(/dev ...
- (转)A Beginner's Guide To Understanding Convolutional Neural Networks
Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolution ...
- Linux-TCP/IP TIME_WAIT状态原理
TIME_WAIT状态原理---------------------------- 通信双方建立TCP连接后,主动关闭连接的一方就会进入TIME_WAIT状态. 客户端主动关闭连接时,会发送最后一个a ...
- google和ebay微服务经验
摘自:http://www.infoq.com/cn/articles/ecosystems-of-microservices 多元化(polyglot)微服务是终极游戏 大规模系统和多元化微服务最终 ...
- Code First 约定
Code First 约定 借助 Code First,可通过使用 C# 或 Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code Fi ...
- 007. 自定义ListBox的item的宽高, 字体居中
/// <summary> /// 自定义ListBox的item的宽高, 字体居中 /// </summary> /// <param name="sende ...