C#文件压缩成.Zip
使用的三方类库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的更多相关文章
- java实现将文件压缩成zip格式
以下是将文件压缩成zip格式的工具类(复制后可以直接使用): zip4j.jar包下载地址:http://www.lingala.net/zip4j/download.php package util ...
- python(49):把文件压缩成zip格式的文件
有时需要用到压缩文件,网上搜集了一段代码: 分享一下: import os import zipfile def make_zip(localPath, pname): zipf = zipfile. ...
- 列出zip文件内全部内容 当前目录下的所有文件压缩成zip格式的文件(file.zip)
[root@ok Desktop]# zip -r image.zip ./*.jpg adding: 20161007_113743.jpg (deflated 0%) adding: 201610 ...
- Java实现将文件或者文件夹压缩成zip
最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类.但找了好多篇博客,总是发现有bug.因此就自己来写了个工具类. 这个工具类的功能为: ( ...
- vue-webpack项目自动打包压缩成zip文件批处理
为什么需要这个? 使用vue框架开发项目,npm run build这个命令会一直用到,如果需要给后端发包,那你还要打包成zip格式的压缩包,特别是项目提测的时候,一天可能要执行重复好几次,所以才有了 ...
- java将文件打包成ZIP压缩文件的工具类实例
package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
- linux下压缩成zip文件解压zip文件
linux zip命令的基本用法是: zip [参数] [打包后的文件名] [打包的目录路径] linux zip命令参数列表: -a 将文件转成ASCII模式 -F 尝试修复损坏 ...
- 【转】Java实现将文件或者文件夹压缩成zip
转自:https://www.cnblogs.com/zeng1994/p/7862288.html package com.guo.utils; import java.io.*; import j ...
- asp.net 把图片压缩成zip之后再进行下载
//这是导出的js方法 function fundaochu() { var data = "keyword=GetImageListdaochu&type=daochu&m ...
随机推荐
- 搭建SVN服务器时报错:0x80004002
一.错误信息 Cannot query proxy blanket: no such interface supported (0x80004002) 二.解决方案 这个错误只会在有NVIDIA独立显 ...
- 描述一下 Intent 和 IntentFilter?
Android 中通过 Intent 对象来表示一条消息,一个 Intent 对象不仅包含有这个消息的目的地,还可以包含消息的内容,这好比一封 Email,其中不仅应该包含收件地址,还可以包含具体的内 ...
- C# 实现播放RTSP 标准协议码流播放
http://www.codeproject.com/Articles/507218/Managed-Media-Aggregation-using-Rtsp-and-Rtphttp://www.st ...
- 使用多个fixture和fixture直接互相调用
使用多个fixture 如果用例需要用到多个fixture的返回数据,fixture也可以return一个元组.list或字典,然后从里面取出对应数据. # test_fixture4.py impo ...
- CSS元素隐藏
{ display: none; /* 不占据空间,无法点击 */ } /*************************************************************** ...
- Pytorch笔记 (3) 科学计算2
一.组织张量的元素 (1)重排张量元素 本节介绍在不改变 张量元素个数 和 各元素的值的情况下改变张量的大小 torch.Tensor类的成员方法 reshape() 参数是多个int类型的值. 如果 ...
- PEP8-python编码规范(上)
包含主要 Python 发行版中的标准库的 Python 代码的编码约定. 1.代码缩进 (1)每个缩进需要使用 4 个空格.一般使用一个Tab键. Python 3 不允许混合使用制表符和空格来缩进 ...
- 拼音检查python
#coding=utf-8 #!/usr/bin/python import sys, re, collections #读入文件 def read_file(filename): try: fp = ...
- UVA 1642 MagicalGCD 题解
题面 本题是一道区间最大公约数的模板题: 如果N^2暴力的话当然会超时,所以我们要发掘出区间gcd的特点: 设gcd[i]表示区间[1,i]的最大公约数: 我们可以发现,从一个点i到1之间的所有区间的 ...
- php中文网--JavaScript
PHP中文网:http://www.php.cn/course/18.html 常用的两个客户端输出方法 document.write("你好呀js"); 描述:在网页的<b ...