最近想用个解压缩功能 从网上找了找 加自己修改,个人感觉还是比较好用的,直接上代码如下

using System;
using System.Linq;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.Diagnostics;
using Microsoft.Win32; namespace ZipCommon
{
public class ZipHelper
{ #region 压缩多个文件 /// <summary>
/// 压缩多个文件
/// </summary>
/// <param name="files">文件名</param>
/// <param name="ZipedFileName">压缩包文件名</param>
/// <param name="Password">解压码</param>
/// <returns></returns>
public static void Zip(string[] files, string ZipedFileName, string Password)
{
files = files.Where(f => File.Exists(f)).ToArray();
if (files.Length == 0) throw new FileNotFoundException("未找到指定打包的文件");
ZipOutputStream s = new ZipOutputStream(File.Create(ZipedFileName));//此处可以设置 文件暂存服务器盘符的位置 c:/ + zipFildName
s.SetLevel(6);
if (!string.IsNullOrEmpty(Password.Trim())) s.Password = Password.Trim();
ZipFileDictory(files, s);
s.Finish();
s.Close();
} /// <summary>
/// 压缩多个文件
/// </summary>
/// <param name="files">文件名</param>
/// <param name="ZipedFileName">压缩包文件名</param>
/// <returns></returns>
public static void Zip(string[] files, string ZipedFileName)
{
Zip(files, ZipedFileName, string.Empty);
} private static void ZipFileDictory(string[] files, ZipOutputStream s)
{
ZipEntry entry = null;
FileStream fs = null;
Crc32 crc = new Crc32();
try
{
//创建当前文件夹
entry = new ZipEntry("../"); //加上 “/” 才会当成是文件夹创建 加两点和才是在当前目录下压缩 不然会多出一个文件夹
s.PutNextEntry(entry);
s.Flush();
foreach (string file in files)
{
//打开压缩文件
fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry = new ZipEntry("/" + Path.GetFileName(file));
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, 0, buffer.Length);
}
}
finally
{
if (fs != null)
{
fs.Close();
fs = null;
}
if (entry != null)
entry = null;
GC.Collect();
}
} #endregion 压缩多个文件 #region 解压文件 包括.rar 和zip /// <summary>
///解压文件
/// </summary>
/// <param name="fileFromUnZip">解压前的文件路径(绝对路径)</param>
/// <param name="fileToUnZip">解压后的文件目录(绝对路径)</param>
public static void UnpackFileRarOrZip(string fileFromUnZip, string fileToUnZip)
{
//获取压缩类型
string unType = fileFromUnZip.Substring(fileFromUnZip.LastIndexOf(".") + 1, 3).ToLower(); switch (unType)
{
case "rar":
UnRar(fileFromUnZip, fileToUnZip);
break;
default:
UnZip(fileFromUnZip, fileToUnZip);
break; }
} #endregion #region 解压文件 .rar文件 /// <summary>
/// 解压
/// </summary>
/// <param name="unRarPatch"></param>
/// <param name="rarPatch"></param>
/// <param name="rarName"></param>
/// <returns></returns>
public static void UnRar(string fileFromUnZip, string fileToUnZip)
{
string the_rar;
RegistryKey the_Reg;
object the_Obj;
string the_Info; try
{
the_Reg = Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
//the_rar = the_rar.Substring(1, the_rar.Length - 7); if (Directory.Exists(fileToUnZip) == false)
{
Directory.CreateDirectory(fileToUnZip);
}
the_Info = "x " + Path.GetFileName(fileFromUnZip) + " " + fileToUnZip + " -y"; ProcessStartInfo the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = Path.GetDirectoryName(fileFromUnZip);//获取压缩包路径 Process the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
catch (Exception ex)
{
throw ex;
}
//return unRarPatch;
} #endregion #region 解压文件 .zip文件 /// <summary>
/// 解压功能(解压压缩文件到指定目录)
/// </summary>
/// <param name="FileToUpZip">待解压的文件</param>
/// <param name="ZipedFolder">指定解压目标目录</param>
public static void UnZip(string FileToUpZip, string ZipedFolder)
{
if (!File.Exists(FileToUpZip))
{
return;
} if (!Directory.Exists(ZipedFolder))
{
Directory.CreateDirectory(ZipedFolder);
} ICSharpCode.SharpZipLib.Zip.ZipInputStream s = null;
ICSharpCode.SharpZipLib.Zip.ZipEntry theEntry = null; string fileName;
FileStream streamWriter = null;
try
{
s = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(File.OpenRead(FileToUpZip));
while ((theEntry = s.GetNextEntry()) != null)
{ if (theEntry.Name != String.Empty)
{
fileName = Path.Combine(ZipedFolder, theEntry.Name);
///判断文件路径是否是文件夹 if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
continue;
} streamWriter = File.Create(fileName);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
finally
{
if (streamWriter != null)
{
streamWriter.Close();
streamWriter = null;
}
if (theEntry != null)
{
theEntry = null;
}
if (s != null)
{
s.Close();
s = null;
}
GC.Collect();
GC.Collect(1);
}
} #endregion
}
}

批量压缩文件.net的更多相关文章

  1. python实现批量压缩文件夹

    前段时间碰到一个需要把目录下文件夹压缩的项目,但是度娘里没找到,只好自己写脚本了. #coding:utf-8 import os filePath = raw_input("请输入路径:& ...

  2. 使用7zip批量压缩文件夹到不同压缩包

    for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\" ...

  3. java使用ZipOutputStream批量压缩文件,并将文件分别放置不同文件夹压缩

    package cn.cnnho.backstage.controller;import java.util.ArrayList; import java.util.List; import java ...

  4. linux shell 完成批量压缩文件

    首先得到文件列表 使用 list -1 注意是1 不是l 然后是用一个循环内包装zip代码 #!/bin/bash list=`` for var in $list do echo $var zip ...

  5. 通过jpegoptim批量压缩文件

    #!/bin/sh filelist=$(ls) for file in $filelist do if [ -d $file ] then du -h $file /usr/local/bin/jp ...

  6. tcl实现批量压缩文件夹

    tcl脚本本身对字符串的处理比较简单,所以想着用这个也实现下: proc main {} { puts "请输入路径:" set strpath "E:\\123&quo ...

  7. 批量压缩文件夹到Zip文件

    实现效果: 实现代码:

  8. 简单测试Demo:如何用Java压缩文件夹和文件

    一.直接贴出测试代码 package com.jason.zip; import java.io.File; import java.io.FileInputStream; import java.i ...

  9. Jsp实现筛选并压缩文件批量下载

    Jsp实现筛选并压缩文件批量下载 首先明确一下需求,网页端点击一下button,传递特定的参数到download.jsp网页,筛选文件,对过滤得到的文件进行压缩,然后返回前端一个压缩包下载. 以下的代 ...

随机推荐

  1. hanlp中文自然语言处理的几种分词方法

    自然语言处理在大数据以及近年来大火的人工智能方面都有着非同寻常的意义.那么,什么是自然语言处理呢?在没有接触到大数据这方面的时候,也只是以前在学习计算机方面知识时听说过自然语言处理.书本上对于自然语言 ...

  2. select选中事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Sigar简介

    大家好,我是Sigar.也许好多人还不认识我.下面就介绍一下我自己,好让大家对我有一个大致的了解. 我的全名是System Information Gatherer And Reporter,中文名是 ...

  4. bzoj 4929: 第三题

    Description 给定n,b,c,d,e以及A0,A1,···,An−1,定义 xk=b×c^4k+d×c^2k+e f(x)=Sigma(Aix^i),0<=i<=n-1 请你求出 ...

  5. centos 安装卸载软件命令 & yum安装LAMP环境

    安装一个软件时 yum -y install httpd 安装多个相类似的软件时 yum -y install httpd* 安装多个非类似软件时 yum -y install httpd php p ...

  6. Leetcode 之Simplify Path @ python

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  7. 在win7/WINDOWS SERVER 2008 R2上安装 vmware POWERcli 6.5

    安装.NET Framework 4.6.2下载NDP462-KB3151800-x86-x64-AllOS-ENU.exe,安装安装PowerShell 4.0(5.0依赖4.0)下载Windows ...

  8. OpenGL 画出雷达动态扫描效果(一)

    最终效果如下所示 Demo下载  http://files.cnblogs.com/xd-jinjian/Debug.zip 源代码下载 http://download.csdn.net/detail ...

  9. 比较有意思的原生态js拖拽写法----摘自javascript高级程序设计3

    var DragDrop = function () { var dragging = null; var diffX = 0; var diffY = 0; function handleEvent ...

  10. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #17 如何使用ext4

    HACK #17 如何使用ext4 本节介绍ext4的编写和挂载方法.开发版ext4的使用方法.ext4是ext3的后续文件系统,从Linux 2.6.19开始使用.现在主要的发布版中多数都是采用ex ...