C#压缩文件,C#压缩文件夹,C#获取文件
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
namespace BLL
{
/// <summary>
/// 文件(夹)压缩、解压缩
/// </summary>
public class FileCompression
{
#region 压缩文件
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileNames">要打包的文件列表</param>
/// <param name="GzipFileName">目标文件名</param>
/// <param name="CompressionLevel">压缩品质级别(0~9)</param>
/// <param name="deleteFile">是否删除原文件</param>
public static void CompressFile(List<FileInfo> fileNames, string GzipFileName, int CompressionLevel, bool deleteFile)
{
ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
try
{
s.SetLevel(CompressionLevel); //0 - store only to 9 - means best compression
foreach (FileInfo file in fileNames)
{
FileStream fs = null;
try
{
fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
}
catch
{ continue; }
// 方法二,将文件分批读入缓冲区
byte[] data = new byte[];
int size = ;
ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name));
entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime);
s.PutNextEntry(entry);
while (true)
{
size = fs.Read(data, , size);
if (size <= ) break;
s.Write(data, , size);
}
fs.Close();
if (deleteFile)
{
file.Delete();
}
}
}
finally
{
s.Finish();
s.Close();
}
}
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="dirPath">要打包的文件夹</param>
/// <param name="GzipFileName">目标文件名</param>
/// <param name="CompressionLevel">压缩品质级别(0~9)</param>
/// <param name="deleteDir">是否删除原文件夹</param>
public static void CompressDirectory(string dirPath, string GzipFileName, int CompressionLevel, bool deleteDir)
{
//压缩文件为空时默认与压缩文件夹同一级目录
if (GzipFileName == string.Empty)
{
GzipFileName = dirPath.Substring(dirPath.LastIndexOf("//") + );
GzipFileName = dirPath.Substring(, dirPath.LastIndexOf("//")) + "//" + GzipFileName + ".zip";
}
//if (Path.GetExtension(GzipFileName) != ".zip")
//{
// GzipFileName = GzipFileName + ".zip";
//}
using (ZipOutputStream zipoutputstream = new ZipOutputStream(File.Create(GzipFileName)))
{
zipoutputstream.SetLevel(CompressionLevel);
Crc32 crc = new Crc32();
Dictionary<string, DateTime> fileList = GetAllFies(dirPath);
foreach (KeyValuePair<string, DateTime> item in fileList)
{
FileStream fs = File.OpenRead(item.Key.ToString());
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
ZipEntry entry = new ZipEntry(item.Key.Substring(dirPath.Length));
entry.DateTime = item.Value;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipoutputstream.PutNextEntry(entry);
zipoutputstream.Write(buffer, , buffer.Length);
}
}
if (deleteDir)
{
Directory.Delete(dirPath, true);
}
}
/// <summary>
/// 获取所有文件
/// </summary>
/// <returns></returns>
private static Dictionary<string, DateTime> GetAllFies(string dir)
{
Dictionary<string, DateTime> FilesList = new Dictionary<string, DateTime>();
DirectoryInfo fileDire = new DirectoryInfo(dir);
if (!fileDire.Exists)
{
throw new System.IO.FileNotFoundException("目录:" + fileDire.FullName + "没有找到!");
}
GetAllDirFiles(fileDire, FilesList);
GetAllDirsFiles(fileDire.GetDirectories(), FilesList);
return FilesList;
}
/// <summary>
/// 获取一个文件夹下的所有文件夹里的文件
/// </summary>
/// <param name="dirs"></param>
/// <param name="filesList"></param>
private static void GetAllDirsFiles(DirectoryInfo[] dirs, Dictionary<string, DateTime> filesList)
{
foreach (DirectoryInfo dir in dirs)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName, file.LastWriteTime);
}
GetAllDirsFiles(dir.GetDirectories(), filesList);
}
}
/// <summary>
/// 获取一个文件夹下的文件
/// </summary>
/// <param name="dir">目录名称</param>
/// <param name="filesList">文件列表HastTable</param>
private static void GetAllDirFiles(DirectoryInfo dir, Dictionary<string, DateTime> filesList)
{
foreach (FileInfo file in dir.GetFiles("*.*"))
{
filesList.Add(file.FullName, file.LastWriteTime);
}
}
#endregion
#region 解压缩文件
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="GzipFile">压缩包文件名</param>
/// <param name="targetPath">解压缩目标路径</param>
public static void Decompress(string GzipFile, string targetPath)
{
//string directoryName = Path.GetDirectoryName(targetPath + "//") + "//";
string directoryName = targetPath;
if (!Directory.Exists(directoryName)) Directory.CreateDirectory(directoryName);//生成解压目录
string CurrentDirectory = directoryName;
byte[] data = new byte[];
int size = ;
ZipEntry theEntry = null;
using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
{
while ((theEntry = s.GetNextEntry()) != null)
{
if (theEntry.IsDirectory)
{// 该结点是目录
if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
}
else
{
if (theEntry.Name != String.Empty)
{
// 检查多级目录是否存在
if (theEntry.Name.Contains("//"))
{
string parentDirPath = theEntry.Name.Remove(theEntry.Name.LastIndexOf("//") + );
if (!Directory.Exists(parentDirPath))
{
Directory.CreateDirectory(CurrentDirectory + parentDirPath);
}
} //解压文件到指定的目录
using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
{
while (true)
{
size = s.Read(data, , data.Length);
if (size <= ) break;
streamWriter.Write(data, , size);
}
streamWriter.Close();
}
}
}
}
s.Close();
}
}
#endregion
}
}
C#压缩文件,C#压缩文件夹,C#获取文件的更多相关文章
- Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录
Java IO,io,文件操作,删除文件,删除文件夹,获取文件父级目录 这里先简单的贴下常用的方法: File.separator //当前系统文件分隔符 File.pathSeparator // ...
- Java中递归的优缺点,Java写一个递归遍历目录下面的所有文件包括子文件夹里边的文件。
题目: 遍历出aaa文件夹下的文件 首先分析思路: 1.首先判断这个文件夹是否为文件,通过isFile()函数可以判断是否为文件. 2.然后通过isDirectory判断是否为目录. 3.如果是目录就 ...
- php遍历一个文件下的所有文件和子文件夹下的文件
function AllFile($dir){ if($dh = opendir($dir)){ while (($file = readdir($dh)) !== false){ if($file ...
- MFC下打开选择文件夹并获取文件夹的绝对路径
http://blog.csdn.net/w18758879921/article/details/51613382 http://www.cnblogs.com/greatverve/archive ...
- C# 递归查找文件夹下所有文件和子文件夹的所有文件
方法实现 public class DirectoryAllFiles { static List<FileInformation> FileList = new List<File ...
- c#下载共享文件夹下的文件并记录错误日志
public void Run() { //获取目标文件列表 string _ErrorMessage = ""; string _ErrorMessageFile = " ...
- sharepoint REST API 获取文件夹及文件
使用REST操作文件夹: 获取文件夹 url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')/f ...
- 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件
[源码下载] 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件 作者:webabcd 介绍背水一战 Windows 10 之 ...
- 使用idea写ssm的时候提示源文件夹中的文件找不到
<context:property-placeholder location="classpath:db.properties"/>这一行idea提示找不到db.pro ...
随机推荐
- Keuskal算法模板
int cmp(const int i, const int j) { return w[i]<w[j]; }///间接比较函数,w[i]表示边i权值 int find_set(int x) { ...
- python3-datetime.date详解(一)
datetime是python操作日期和时间的内置模块. python有两种日期.时间对象:“naive”和“aware”.前者由于忽略了实际情况更容易理解,操作.在任何时间空间内,它的值都取决于一个 ...
- AssetBundle打包-----BuildPipeline的应用
打包思路:确定要打包资源的路径.和打包的输出路径(一般为S路径),把存放资源的路径使用递归进行遍历,获取所有资源,文件类型的资源可以通过File拷贝或IO写到输出路径,其他资源的打包通过AssetBu ...
- Python3数据类型及转换
I. 数据类型 Python3将程序中的任何内容统称为对象(Object),基本的数据类型有数字和字符串等,也可以使用自定义的类(Classes)创建新的类型. Python3中有六个标准的数据类型: ...
- 前端基础:form表单提交
今天介绍下form表单提交经常用到的表单元素. 1:datalist元素,一般与input组建配合使用,以定义可能输入的值,例如: <!DOCTYPE html> <html lan ...
- visual studio 2017 中默认无法开发 Android 8.0 及以上系统的解决方案
一般默认比较旧有两个原因,系统版本过旧,Visual Studio 版本过旧. 第一步,将windows 更新到最新版,必须是windows 10 并且更新到最新. 第二步,将visual studi ...
- Linux----------mysql进阶
目录 一.破解密码以及无密码登录 1.1 破解密码 1.2 无密码登录 1.3 定义不同的客户端 1.4 家目录下 二.视图 三.函数 3.1 系统函数 3.2 自定义函数 3.3 自定义函数中定义局 ...
- postman进行https接口测试所遇到的ssl证书问题,参考别人方法
参考文档: https://learning.getpostman.com/docs/postman/sending_api_requests/certificates/ 随着 https 的推动,更 ...
- java自动更新问题
第一次运行公司erp,打开的是jnlp文件,在弹出的第一个框上,同事点了更新java,后面悲剧了,再也没有办法打开erp了,直接跳到java官网上要求更新,而erp在java8上有一些功能不兼容,所以 ...
- 剑指offer 12.代码的完整性 数值的整数次方
题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 本人渣渣代码: public double Power(double ba ...