压缩文件夹

源码如下
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI; using System.IO; using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip; /// /// ZipFloClass 的摘要说明
/// public class ZipFloClass
{
public void ZipFile(string strFile, string strZip)
{
if (strFile[strFile.Length - ] != Path.DirectorySeparatorChar)
strFile += Path.DirectorySeparatorChar;
ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
s.SetLevel(); // 0 - store only to 9 - means best compression
zip(strFile, s, strFile);
s.Finish();
s.Close();
} private void zip(string strFile, ZipOutputStream s, 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))
{
zip(file, s, staticFile);
} else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length);
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;
s.PutNextEntry(entry); s.Write(buffer, , buffer.Length);
}
}
} } 解压文件夹 using System;
using System.Data;
using System.Web;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary; using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums; /// /// UnZipFloClass 的摘要说明
/// public class UnZipFloClass
{
public string unZipFile(string TargetFile, string fileDir)
{
string rootFile = " ";
try
{
//读取压缩文件(zip文件),准备解压缩
ZipInputStream s = new ZipInputStream(File.OpenRead(TargetFile.Trim()));
ZipEntry theEntry;
string path = fileDir;
//解压出来的文件保存的路径 string rootDir = " ";
//根目录下的第一个子文件夹的名称
while ((theEntry = s.GetNextEntry()) != null)
{
rootDir = Path.GetDirectoryName(theEntry.Name);
//得到根目录下的第一级子文件夹的名称
if (rootDir.IndexOf("\\") >= )
{
rootDir = rootDir.Substring(, rootDir.IndexOf("\\") + );
}
string dir = Path.GetDirectoryName(theEntry.Name);
//根目录下的第一级子文件夹的下的文件夹的名称
string fileName = Path.GetFileName(theEntry.Name);
//根目录下的文件名称
if (dir != " " )
//创建根目录下的子文件夹,不限制级别
{
if (!Directory.Exists(fileDir + "\\" + dir))
{
path = fileDir + "\\" + dir;
//在指定的路径创建文件夹
Directory.CreateDirectory(path);
}
}
else if (dir == " " && fileName != "")
//根目录下的文件
{
path = fileDir;
rootFile = fileName;
}
else if (dir != " " && fileName != "")
//根目录下的第一级子文件夹下的文件
{
if (dir.IndexOf("\\") > )
//指定文件保存的路径
{
path = fileDir + "\\" + dir;
}
} if (dir == rootDir)
//判断是不是需要保存在根目录下的文件
{
path = fileDir + "\\" + rootDir;
} //以下为解压缩zip文件的基本步骤
//基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(path + "\\" + 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(); return rootFile;
}
catch (Exception ex)
{
return "1; " + ex.Message;
}
}
}

调用:

/// <summary>
/// 压缩
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string[] FileProperties = new string[];
FileProperties[] = "D:\\Debug\\";//待压缩文件目录
//Debug2=>目录必须存在(可以改进程序)
FileProperties[] = "D:\\Debug2\\a.zip"; //压缩后的目标文件
ZipFloClass Zc = new ZipFloClass();
Zc.ZipFile(FileProperties[], FileProperties[]);
} /// <summary>
/// 解压
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
string[] FileProperties = new string[];
FileProperties[] = "D:\\Debug2\\a.zip";//待解压的文件
//=>Debug3目录可以不用存在
FileProperties[] = "D:\\Debug3\\";//解压后放置的目标目录
UnZipFloClass UnZc = new UnZipFloClass();
UnZc.unZipFile(FileProperties[], FileProperties[]);
}
 ICSharpCode.SharpZipLib.dll下载原文地址:http://www.cnblogs.com/zfanlong1314/p/4202695.htmlPS.补充RAR解压 2018-8-18
SharpCompress的C#框架被开源,您可以在搜索引擎中找到SharpCompress框架的开源代码,它支持:rar 7zip, zip, tar, tzip和bzip2格式的压缩和解压,下面的示例直接从rar格式文件读取并解压文件。
using (Stream stream = File.OpenRead(@"C:\Code\sharpcompress.rar"))
{
var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(@"C:\temp");
}
}
}
												

【转】【C#】ZIP、RAR 压缩与解压缩的更多相关文章

  1. nodejs rar/zip加密压缩、解压缩

    1.shell/cmd命令行压缩解压缩 (1)zip压缩解压缩 zip压缩:zip -rP{密码} <目标文件.zip> <源文件> //默认覆盖现有文件 zip解压缩:zip ...

  2. 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩

    使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...

  3. 利用Java进行zip文件压缩与解压缩

    摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...

  4. zip格式压缩、解压缩(C#)

    压缩方法 #region 压缩 /// <summary> /// 压缩 /// </summary> /// <param name="bytes" ...

  5. Linux之文件的压缩与解压缩

    压缩格式 .zip,.rar,.7z,.tar,.gz,.xz,.bz2,.tar.gz,.tar.xz,.tar.bz2,其中,形如*.tar.gz为tar打包,gz压缩的文件 zip压缩打包程序 ...

  6. C#压缩、解压缩文件(夹)(rar、zip)

    主要是使用Rar.exe压缩解压文件(夹)(*.rar),另外还有使用SevenZipSharp.dll.zLib1.dll.7z.dll压缩解压文件(夹)(*.zip).需要注意的几点如下: 1.注 ...

  7. Mac上zip,rar,tar文件命令解压和压缩

    经常遇到在windowns上的压缩文件,在mac上解压出现问题,特意总结了下在Terminal里常用命令的方式解压和压缩文件 1.zip压缩文件 zip命令的参数很多,可以利用"zip -- ...

  8. zip,rar及linux下常用的压缩格式

    日常操作中我们经常使用到文件压缩操作,其使用一些特定的算法来减小文件的大小,可以提高传输数据时的速率和减少数据在一些存储机制上占有的空间大小,实现空间利用最大化. 比如:如果你想通过邮箱发送一个文件夹 ...

  9. zip压缩与解压缩示例

    范例: zip命令可以用来将文件压缩成为常用的zip格式.unzip命令则用来解压缩zip文件. 1. 我想把一个文件abc.txt和一个目录dir1压缩成为yasuo.zip: # zip -r y ...

随机推荐

  1. JAVA-day08 下午-总结、測试

    继承总结: class { public static void main(String[] args) { System.out.println("Hello World!"); ...

  2. js 与 php 时间戳的区别(毫秒与秒的计算方式)

    js是以毫秒为单位计算的,php是以秒为单位计算的,所以转换时记得*/1000 //距离时间的时间戳 var suoshengshijian = <?php echo $expire_time_ ...

  3. Meta Blogging

    Meta Blogging 由来 偶然想到说如果哪一天cnblogs挂了,那之前记录的随笔怎么办?可不可以把它们给download下来本地保存一份.正好看到有个库叫requests, 干嘛不试试看呢. ...

  4. jquery动态加载js三种方法实例

    这里为你提供了三种动态加载js的jquery实例代码哦,由于jquery是为用户提供方便的,所以利用jquery动态加载文件只要一句话$.getScript(\"test.js\" ...

  5. [k8s]subpath解决cm覆盖目录问题

    参考 发现cm老覆盖容器原有目录里的内容,后来不得不通过in -s的方式来搞cm, 先将cm挂到/tmp下,然后ln -s文件到指定目录. 后来发现个cm的subpath特性可以解决这个问题 写了bu ...

  6. WebService之CXF注解之四(測试类)

    TeacherTest.java: /** * @Title:TeacherTest.java * @Package:com.test.service * @Description: * @autho ...

  7. Set 集合论

    https://en.m.wikipedia.org/wiki/De_Morgan%27s_laws https://plato.stanford.edu/entries/set-theory/ ht ...

  8. Oracle PLSQL Demo - 27.Declare & Run Sample

    declare v_sal ) :; begin --if you could not see the output in console, you should set output on firs ...

  9. python 类的使用实例方法

    class A: dict_1 = 456 def __init__(self): self.dict_1 = 'abcdsafdsf' #当增加@staticmethod 时为类方法 @static ...

  10. 创建一个动态链接库 (DLL),使用VS2010

    在本演练中,您将创建一个动态链接库 (DLL),其中包含可供其他应用程序使用的有用例程.使用 DLL 是一种重用代码的绝佳方式.您不必在自己创建的每个程序中重新实现这些例程,而只需对这些例程编写一次, ...