【【【【C#压缩文件】】】】
方法1:
//【filepath想要压缩文件的地址】
//【zippath输出压缩文件的地址】
private void GetFileToZip(string filepath,string zippath)
{ FileStream fs = File.OpenRead(filepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close(); FileStream ZipFile = File.Create(zippath);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry(输出的文件名称);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6); ZipStream.Write(buffer, 0, buffer.Length);
ZipStream.Finish();
ZipStream.Close();
}
方法2:
private void FileToZip(string path,string address)
{
string name = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("\\")+1);
FileStream StreamToZip = new FileStream(path, FileMode.Open, FileAccess.Read);
FileStream ZipFile = File.Create(address);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
//压缩文件
ZipEntry ZipEntry = new ZipEntry(name);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6);
byte[] buffer = new byte[StreamToZip.Length];
StreamToZip.Read(buffer, 0, Convert.ToInt32(StreamToZip.Length));
ZipStream.Write(buffer, 0, Convert.ToInt32(StreamToZip.Length));
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close(); }
【【【【【【C#解压文件】】】】】】
private void ZipToFile(string path, string addres)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(path));
ZipEntry fileEntry;
while ((fileEntry = s.GetNextEntry()) != null)
{
string filename = Path.GetFileName(fileEntry.Name);
if (filename != "")
{
filename = addres + "\\" + filename;
FileStream streamWriter = File.Create(filename);
int size = 2048;
byte[] buffer = new byte[s.length]; size = s.Read(buffer, 0, size);
streamWriter.Write(buffer, 0, size);
streamWriter.Close();
}
}
s.Close();
}
【【【【【【C#压缩目录】】】】】】
方法1:
//【arg[0]要压缩的目录】
//【arg[1]输出的压缩文件】
private void DirectoryToZip(string path, string address)
{
//获取当前文件夹中所有的文件
string[] filenames = Directory.GetFiles(path);
Crc32 crc = new Crc32();
//创建输出文件(ZIP格式的文件)
ZipOutputStream zos = new ZipOutputStream(File.Create(address));
zos.SetLevel(6);
//遍历所有的文件
foreach (string name in filenames)
{
FileStream fs = File.OpenRead(name);
byte[] buffer = new byte[fs.Length];
//读取文件
fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
//获取文件的文件名称和后缀名
string file = Path.GetFileName(name);
//输出文件的名称
ZipEntry entry = new ZipEntry(file);
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zos.PutNextEntry(entry);
zos.Write(buffer, 0, Convert.ToInt32(fs.Length));
fs.Close();
}
zos.Finish();
zos.Close();
} 【【【【【【【【C#读取压缩文件(将压缩文件转换为二进制)】】】】】】】】
private void GetZipToByte(){
string path = @"C:\Documents and Settings\Administrator\桌面\文件.rar";
FileStream fs = new FileStream(path, FileMode.Open);
bytes = new byte[fs.Length];
int count = Convert.ToInt32(fs.Length);
fs.Read(bytes, 0, count);
}
【【【【【【【【C#将二进制转换为压缩文件】】】】】】】】
private void GetByteToZip()
{
string path = @"F:\dom.rar";//压缩文件的地址
File.WriteAllBytes(path, bytes);
}

摘:C#压缩文件的更多相关文章

  1. 【VC++技术杂谈008】使用zlib解压zip压缩文件

    最近因为项目的需要,要对zip压缩文件进行批量解压.在网上查阅了相关的资料后,最终使用zlib开源库实现了该功能.本文将对zlib开源库进行简单介绍,并给出一个使用zlib开源库对zip压缩文件进行解 ...

  2. C#基础-压缩文件及故障排除

    C#压缩文件可以使用第三方dll库:ICSharpCode.SharpZipLib.dll: 以下代码能实现文件夹与多个文件的同时压缩.(例:把三个文件夹和五个文件一起压缩成一个zip) 直接上代码, ...

  3. 破解压缩文件密码rarcrack

    破解压缩文件密码rarcrack   常见的压缩文件格式有ZIP.RAR和7z.这三种格式都支持使用密码进行加密压缩.前面讲过破解ZIP压缩文件,可以使用fcrackzip.对于RAR和7z格式,可以 ...

  4. 文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件

    总结:                                                          使用PHP下载文件的操作需要给出四个header(),可以参考我的另一篇博文: ...

  5. java ZipOutputStream压缩文件,ZipInputStream解压缩

    java中实现zip的压缩与解压缩.java自带的 能实现的功能比较有限. 本程序功能:实现简单的压缩和解压缩,压缩文件夹下的所有文件(文件过滤的话需要对File进一步细节处理). 对中文的支持需要使 ...

  6. C# 压缩文件与字节互转

    public class ZipBin { public byte[] bytes; //C#读取压缩文件(将压缩文件转换为二进制 public void GetZipToByte(string in ...

  7. Linux如何复制,打包,压缩文件

    (1)复制文件 cp -r  要copy的文件/("/"指的是包括里面的内容)   newfile_name(要命名的文件名) eg:cp -r webapps_zero/   f ...

  8. java生成压缩文件

    在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载.所以自己写了一个压缩文件的工具类.该工具类支持单个文件和文件夹压缩.放代码: import java.io.BufferedOutput ...

  9. 7z压缩文件时排除指定的文件

    分享一个7z压缩文件时排除指定文件类型的命令行,感觉很有用: 7z a -t7z d:\updateCRM.7z d:\updateCRM\*.* -r -x!*.log -x!*bak a:创建压缩 ...

随机推荐

  1. Digital controller compensates analog controller

    Emerging digital ICs for power control lack basic features, such as the built-in gate drive and curr ...

  2. 搭建java开发环境:安装JDK

    一:下载安装JDK,java7相对稳定成熟 (下载地址>>) 必须选择同意,然后根据自己的系统版本选择对应的jdk 如: 装完jdk就会提示安装jre,一般放同一目录下 配置系统变量: w ...

  3. MS Sql Server 中主从库的配置和使用介绍

    网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库. 主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据 ...

  4. web小流量实验方案

    近期在思考怎么做小流量,在网上搜了一下,总结例如以下: 1.前提,站点pv已经达到了一定的规模,比方上百万pv,不做小流量冒然更新功能,可能会带来大面积流量损失.在这样的前提下须要做小流量实验 2.什 ...

  5. linux如何后台运行进程,而且不随终端关闭而关闭

    参考:http://www.cnblogs.com/kaituorensheng/p/3980334.html 使用命令nohup:no hang up,表示关闭终端后,进程并不随着终端关闭而关闭 f ...

  6. 使用Vue.js制作仿Metronic高级表格(一)静态设计

    Metronic高级表格是Metonic框架中自行实现的表格,其底层是Datatables.本教程将主要使用Vue实现交互部分,使用Bootstrap做样式库.jQuery做部分用户交互(弹窗). 使 ...

  7. Zookeeper常用命令 (转)

    原文链接:ZooKeeper系列之二:Zookeeper常用命令 ZooKeeper服务命令: 在准备好相应的配置之后,可以直接通过zkServer.sh 这个脚本进行服务的相关操作 1. 启动ZK服 ...

  8. DICOMDIR结构

    DICOMDIR文件结构: 格式: DICOMDIR文件遵照DICOM文件的存储格式,需要有128个无意义字节的前言(假如没有特殊要求,这些字节都为00H),紧接着是DICOM前缀(长度为4字节的“D ...

  9. 82. Remove Duplicates from Sorted List II && i

    题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...

  10. [Angular-Scaled web] 3. Basic State with ui-router

    1. Install ui-route, include js file in html and add dependence in js file. bower install angular-ui ...