C#压缩指定的文件并生成zip文件

摘要:
ZipOutputStream是OutputStream的一个子类。常用操作方法见下表。ZipOutputStream类方法类型描述1公共ZipOutput的通用方法编号
程序集需要添加的引用是:ICSharpCode.SharpZipLib.dll
使用实例:
            //执行压缩
ClsZip.Zip(downZipPath, sourceTopPath, 1, "", allFilePath);
//执行下载
ctrlDcAllGrxx.download(downZipPath, true);
压缩文件的类ClsZip:
   public class ClsZip
    {
/// <summary>
/// 生成压缩文件
/// </summary>
/// <param name="strZipPath">生成的zip文件的路径,包括文件名</param>
/// <param name="strSourceTopDirectoryPath">要压缩的源文件的上级目录</param>
/// <param name="intZipLevel">T压缩等级</param>
/// <param name="strPassword">压缩包解压密码,不设密码""</param>
/// <param name="filesOrDirectoriesPaths">源文件路径</param>
/// <returns></returns>
public static bool Zip(string strZipPath, string strSourceTopDirectoryPath, int intZipLevel, string strPassword, 
List<string> filesOrDirectoriesPaths)
{
try
{
List<string> AllFilesPath = new List<string>();
if (filesOrDirectoriesPaths.Count > 0) // get all files path
{
for (int i = 0; i < filesOrDirectoriesPaths.Count; i++)
{
if (File.Exists(filesOrDirectoriesPaths[i]))
{
AllFilesPath.Add(filesOrDirectoriesPaths[i]);
}
else if (Directory.Exists(filesOrDirectoriesPaths[i]))
{
GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);
}
}
}
if (AllFilesPath.Count > 0)
{
ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(strZipPath));
zipOutputStream.SetLevel(intZipLevel);
zipOutputStream.Password = strPassword;
for (int i = 0; i < AllFilesPath.Count; i++)
{
string strFile = AllFilesPath[i].ToString();
try
{
if (strFile.Substring(strFile.Length - 1) == "") //folder
{
string strFileName = strFile.Replace(strSourceTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring(1);
}
ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
}
else //file
{
FileStream fs = File.OpenRead(strFile);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string strFileName = strFile.Replace(strSourceTopDirectoryPath, "");
if (strFileName.StartsWith(""))
{
strFileName = strFileName.Substring(0);
}
ZipEntry entry = new ZipEntry(strFileName);
entry.DateTime = DateTime.Now;
zipOutputStream.PutNextEntry(entry);
zipOutputStream.Write(buffer, 0, buffer.Length);
fs.Close();
fs.Dispose();
}
}
catch
{
continue;
}
}
zipOutputStream.Finish();
zipOutputStream.Close();
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
/// <summary>
/// Gets the directory files.
/// </summary>
/// <param name="strParentDirectoryPath">源文件路径</param>
/// <param name="AllFilesPath">所有文件路径</param>
public static void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
{
string[] files = Directory.GetFiles(strParentDirectoryPath);
for (int i = 0; i < files.Length; i++)
{
AllFilesPath.Add(files[i]);
}
string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
for (int i = 0; i < directorys.Length; i++)
{
GetDirectoryFiles(directorys[i], AllFilesPath);
}
if (files.Length == 0 && directorys.Length == 0) //empty folder
{
AllFilesPath.Add(strParentDirectoryPath);
}
}

}
参考:

 ZipOutputStream类(1)

如果要完成一个文件或文件夹的压缩,则要使用ZipOutputStream类。ZipOutputStream是OutputStream的子类,常用操作方法如表所示。

 ZipOutputStream类的常用方法

序号

    

类型

    

1

public ZipOutputStream

(OutputStream out)

构造

创建新的ZIP输出流

2

public void putNextEntry

(ZipEntry e) throws IOException

普通

设置每一个

ZipEntry对象

3

public void setComment

(String comment)

普通

设置ZIP文件的注释

免责声明:文章转载自《C#压缩指定的文件并生成zip文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux 权限命令行 xshell 切换用户Java的静态代码块是否会在类被加载时自动执行?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

chromedriver的使用

1.调用chrome driver System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe"); 2.创建chromeOption对象 ChromeOptions chromeOptio...

c# string.format 中使用$的坑

string a = "hello}"; string s = string.Format($"{a}"); 上面这种情况,会报格式错误,会把hello}中的}当做一个字符串结束; 结果:  修改: string a = "hello}"; string s...

C#版 Tag云图控件

今天看到TerryLee的一篇文章http://www.cnblogs.com/Terrylee/archive/2008/02/20/1075764.html,其中有一条ASP.NET Tag/Search Cloud Server Control(导读:在Web2.0时代,Tag成为了一个标志,通常一些网站都会采用Tag云图来显示。有“好事者”干脆开发...

C#.NET 中的类型转换

C# 出来也有些日子了,最近由于编程的需要,对 C# 的类型转换做了一些研究,其内容涉及 C# 的装箱/拆箱/别名、数值类型间相互转换、字符的 ASCII 码和 Unicode 码、数值字符串和数值之间的转换、字符串和字符数组/字节数组之间的转换、各种数值类型和字节数组之间的转换、十六进制数输出以及日期型数据的一些转换处理,在这里与大家分享—— 1. 装箱...

C++(四十八) — string容器的基本操作

参考博客:https://blog.csdn.net/qq_37941471/article/details/82107077 https://www.cnblogs.com/danielStudy/p/7127564.html#top 1、声明一个字符串 标准库类型string表示可变长的字符序列,为了在程序中使用string类型,我们必须包含头文件:#...

php图片压缩

php图片压缩 能对图片的大小、质量进行压缩。 <?php class ThumbHandler {     var $dst_img;// 目标文件     var $h_src; // 图片资源句柄     var $h_dst;// 新图句柄     var $h_mask;// 水印句柄     var $img_create_quality...