使用的三方类库ICSharpCode.SharpZipLib.dll

方法如下:

 /// <summary>
/// 压缩文件为zip格式
/// </summary>
/// <param name="filepath">文件地址</param>
/// <param name="targetPath">目标路径</param>
static bool CompressFiles(string filePath, string targetPath)
{
try
{
using (ZipOutputStream zipstream = new ZipOutputStream(File.Create(targetPath)))
{
zipstream.SetLevel(); // 压缩级别 0-9
byte[] buffer = new byte[]; //缓冲区大小
ZipEntry entry = new ZipEntry(Path.GetFileName(filePath));
entry.DateTime = DateTime.Now;
zipstream.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(filePath))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, , buffer.Length);
zipstream.Write(buffer, , sourceBytes);
}
while (sourceBytes > );
}
zipstream.Finish();
zipstream.Close();
return true;
}
}
catch (Exception ex)
{
return false;
}
}

压缩文件夹

      /// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="strFile"></param>
/// <param name="strZip"></param>
/// <returns></returns>
public static bool ZipFile(string strFile, string strZip)
{
try
{
if (strFile[strFile.Length - ] != Path.DirectorySeparatorChar)
{
strFile += Path.DirectorySeparatorChar;
}
ZipOutputStream outstream = new ZipOutputStream(File.Create(strZip));
outstream.SetLevel();
ZipCompress(strFile, outstream, strFile);
outstream.Finish();
outstream.Close();
return true;
}
catch (Exception)
{
return false;
}
} public static void ZipCompress(string strFile, ZipOutputStream outstream, string staticFile)
{
if (strFile[strFile.Length - ] != Path.DirectorySeparatorChar)
{
strFile += Path.DirectorySeparatorChar;
}
Crc32 crc = new Crc32();
//获取指定目录下所有文件和子目录文件名称
string[] filenames = Directory.GetFileSystemEntries(strFile);
//遍历文件
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
ZipCompress(file, outstream, staticFile);
}
//否则,直接压缩文件
else
{
//打开文件
FileStream fs = File.OpenRead(file);
//定义缓存区对象
byte[] buffer = new byte[fs.Length];
//通过字符流,读取文件
fs.Read(buffer, , buffer.Length);
//得到目录下的文件(比如:D:\Debug1\test),test
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + );
ZipEntry entry = new ZipEntry(tempfile);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
outstream.PutNextEntry(entry);
//写文件
outstream.Write(buffer, , buffer.Length);
}
}
}

C#文件压缩成.Zip的更多相关文章

  1. java实现将文件压缩成zip格式

    以下是将文件压缩成zip格式的工具类(复制后可以直接使用): zip4j.jar包下载地址:http://www.lingala.net/zip4j/download.php package util ...

  2. python(49):把文件压缩成zip格式的文件

    有时需要用到压缩文件,网上搜集了一段代码: 分享一下: import os import zipfile def make_zip(localPath, pname): zipf = zipfile. ...

  3. 列出zip文件内全部内容 当前目录下的所有文件压缩成zip格式的文件(file.zip)

    [root@ok Desktop]# zip -r image.zip ./*.jpg adding: 20161007_113743.jpg (deflated 0%) adding: 201610 ...

  4. Java实现将文件或者文件夹压缩成zip

            最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类.但找了好多篇博客,总是发现有bug.因此就自己来写了个工具类.         这个工具类的功能为: ( ...

  5. vue-webpack项目自动打包压缩成zip文件批处理

    为什么需要这个? 使用vue框架开发项目,npm run build这个命令会一直用到,如果需要给后端发包,那你还要打包成zip格式的压缩包,特别是项目提测的时候,一天可能要执行重复好几次,所以才有了 ...

  6. java将文件打包成ZIP压缩文件的工具类实例

    package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  7. linux下压缩成zip文件解压zip文件

    linux  zip命令的基本用法是: zip [参数] [打包后的文件名] [打包的目录路径] linux  zip命令参数列表: -a     将文件转成ASCII模式 -F     尝试修复损坏 ...

  8. 【转】Java实现将文件或者文件夹压缩成zip

    转自:https://www.cnblogs.com/zeng1994/p/7862288.html package com.guo.utils; import java.io.*; import j ...

  9. asp.net 把图片压缩成zip之后再进行下载

    //这是导出的js方法 function fundaochu() { var data = "keyword=GetImageListdaochu&type=daochu&m ...

随机推荐

  1. fixture作用范围

    ixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function fixture(scope="f ...

  2. linux计划crontab

    linux计划crontab 启动crontab服务 一般启动服务用  /sbin/service crond start 若是根用户的cron服务可以用 sudo service crond sta ...

  3. Web测试方法_01

    一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直接输入特殊字符时,使 ...

  4. spring cloud依赖服务调用优化

    1.请求缓存 优点: 注解方式实现: 设置缓存key: 如果可以确认,对要缓存的数据的操作,主要是写操作都只在feign调用中完成且读多写少,则可以使用此方式:如果在其他地方还有对数据的写操作,则可能 ...

  5. LeetCode.989-数组形式的整数做加法(Add to Array-Form of Integer)

    这是悦乐书的第371次更新,第399篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第233题(顺位题号是989).对于非负整数X,X的数组形式是从左到右顺序的数字数组.例 ...

  6. MySQL 数据库下载

    地址链接: msi:https://dev.mysql.com/downloads/installer/ zip:https://downloads.mysql.com/archives/commun ...

  7. mapreduce的shufflue过程

    一.Map阶段: a. 文件切片之后,每一个切片对应一个MapTask b. 在MapTask中,默认按行读取,每读取一行,就调用一次map方法 c. map方法在执行的时候会将结果(这个结果中已经包 ...

  8. 基于element表格的合并多个行实例

    官方示例地址:https://github.liubing.me/lb-element-table/zh/guide/ 效果图: 0.下载lb-table 并引入 import LbTable fro ...

  9. C#各种委托介绍

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 一.委托的声明 Delegate Delegate 我们常用到的一种声明 Delegate 至少 ...

  10. Akka系列(三):监管与容错

    前言...... Akka作为一种成熟的生产环境并发解决方案,必须拥有一套完善的错误异常处理机制,本文主要讲讲Akka中的监管和容错. 监管 看过我上篇文章的同学应该对Actor系统的工作流程有了一定 ...