日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SharpZipLib压缩解压

發(fā)布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SharpZipLib压缩解压 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace Test{
??? /// <summary>
??? /// 壓縮
??? /// </summary>
??? public class Compress
??? {
??????? /// <summary>
??????? /// zip路徑
??????? /// </summary>
??????? public class ZipPath
??????? {
??????????? /// <summary>
??????????? /// 是否是文件
??????????? /// </summary>
??????????? public bool IsFile { get; set; }
??????????? /// <summary>
??????????? /// zip中路徑
??????????? /// </summary>
??????????? public string Path { get; set; }
??????????? /// <summary>
??????????? /// 絕對路徑
??????????? /// </summary>
??????????? public string FullPath{get;set;}
??????? }
??????? /// <summary>
??????? /// 遞歸獲取文件目錄
??????? /// 王洪岐 2011-11-22
??????? /// </summary>
??????? /// <param name="arrPath"></param>
??????? /// <param name="listPath"></param>
??????? /// <param name="strMainPath"></param>
????????public static void GetFiles(string[] arrPath, List<ZipPath> listPath, string strMainPath)
??????? {
??????????? foreach (string strN in arrPath)
??????????? {
??????????????? List<string> fileList = Directory.GetFiles(strN).ToList();
??????????????? string[] arrDir=Directory.GetDirectories(strN);
??????????????? if (fileList.Count == 0 && arrDir.Length == 0) listPath.Add(new ZipPath { IsFile = false, Path = strN.Substring(strMainPath.Length + 1) + "\\", FullPath = strN });
??????????????? for (int i = 0; i < fileList.Count; i++)
??????????????? {
??????????????????? listPath.Add(new ZipPath { IsFile = true, Path = fileList[i].Substring(strMainPath.Length + 1), FullPath = fileList[i] });
??????????????? }
??????????????? GetFiles(arrDir, listPath, strMainPath);
??????????? }
??????? }

??????? /// <summary>
??????? /// zip遞歸壓縮
??????? /// 王洪岐 2011-11-22
??????? /// 例:Compress.ZipCompress(new string[] { @"E:\TrainUpdate\ViewWeb\File\SrcSamples\doc", @"E:\素材\icon", @"E:\重要資料\C#\SharpZipLib\壓縮sample.cs" }, Server.MapPath("~/File/a.zip"));
??????? /// </summary>
??????? /// <param name="path">源文件夾路徑</param>
??????? /// <param name="topath">目標文件路徑</param>
??????? /// <param name="FileName">從源文件夾中指定某文件</param>
??????? /// <returns>-1=文件不存在,0=未知錯誤,1=成功</returns>
??????? public static int ZipCompress(string[] arrPath, string topath)
??????? {

??????????? try
??????????? {
??????????????? List<ZipPath> listPath = new List<ZipPath>();
??????????????? foreach (string strN in arrPath)
??????????????? {
??????????????????? string strMainPath = string.Empty;
??????????????????? //文件夾不存在
??????????????????? if (!Directory.Exists(strN) && !File.Exists(strN))
??????????????????? {
??????????????????????? return -1;
??????????????????? }
??????????????????? if (string.IsNullOrEmpty(strMainPath))
??????????????????? {
??????????????????????? strMainPath = Path.GetDirectoryName(strN);
??????????????????? }
??????????????????? //是文件
??????????????????? if (File.Exists(strN))
??????????????????? {
??????????????????????? listPath.Add(new ZipPath { IsFile = true, Path = strN.Substring(strMainPath.Length + 1), FullPath = strN });
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? List<string> fileList = Directory.GetFiles(strN).ToList();
??????????????????????? if (fileList.Count == 0) listPath.Add(new ZipPath { IsFile = false, Path = strN.Substring(strMainPath.Length + 1), FullPath = strN });
??????????????????????? for (int i = 0; i < fileList.Count; i++)
??????????????????????? {
??????????????????????????? listPath.Add(new ZipPath { IsFile = true, Path = fileList[i].Substring(strMainPath.Length + 1), FullPath = fileList[i] });
??????????????????????? }
??????????????????????? GetFiles(Directory.GetDirectories(strN), listPath, strMainPath);
??????????????????? }
??????????????? }
??????????????? using (ZipOutputStream s = new ZipOutputStream(File.Create(topath)))
??????????????? {
??????????????????? s.SetLevel(9); // 0 - store only to 9 - means best compression
??????????????????? byte[] buffer = new byte[4096];
??????????????????? foreach (ZipPath file in listPath)
??????????????????? {
??????????????????????? if (file.IsFile)//是文件
??????????????????????? {
??????????????????????????? ZipEntry entry = new ZipEntry(file.Path);
??????????????????????????? entry.DateTime = DateTime.Now;
??????????????????????????? s.PutNextEntry(entry);
??????????????????????????? using (FileStream fs = File.OpenRead(file.FullPath))
??????????????????????????? {
??????????????????????????????? int sourceBytes;
??????????????????????????????? do
??????????????????????????????? {
??????????????????????????????????? sourceBytes = fs.Read(buffer, 0, buffer.Length);
??????????????????????????????????? s.Write(buffer, 0, sourceBytes);
??????????????????????????????? } while (sourceBytes > 0);
??????????????????????????? }
??????????????????????? }
??????????????????????? else
??????????????????????? {
??????????????????????????? ZipEntry entry = new ZipEntry(file.Path);
??????????????????????????? s.PutNextEntry(entry);
??????????????????????? }
??????????????????? }
??????????????????? s.Finish();
??????????????????? s.Close();
??????????????? }
??????????????? return 1;
??????????? }
??????????? catch
??????????? {
??????????????? return 0;
??????????? }
??????? }
??????? /// <summary>
??????? /// 解壓縮
??????? /// 王洪岐
??????? /// </summary>
??????? /// <param name="filePath"></param>
??????? /// <param name="topath"></param>
??????? /// <returns></returns>
??????? public static int ZipUnCompress(string filePath,string topath)
??????? {
??????????? //文件不存在
??????????? if (!File.Exists(filePath))
??????????? {
??????????????? return -1;
??????????? }
??????????? try
??????????? {
??????????????? using (ZipInputStream s = new ZipInputStream(File.OpenRead(filePath)))
??????????????? {

??????????????????? ZipEntry theEntry;
??????????????????? while ((theEntry = s.GetNextEntry()) != null)
??????????????????? {
??????????????????????? string directoryName = Path.GetDirectoryName(theEntry.Name);
??????????????????????? string fileName = Path.GetFileName(theEntry.Name);

??????????????????????? // create directory
??????????????????????? if (directoryName.Length > 0)
??????????????????????? {
??????????????????????????? Directory.CreateDirectory(topath + directoryName);
??????????????????????? }
??????????????????????? else if (!Directory.Exists(topath))
??????????????????????? {
??????????????????????????? Directory.CreateDirectory(topath);
??????????????????????? }
??????????????????????? if (fileName != String.Empty)
??????????????????????? {
??????????????????????????? using (FileStream streamWriter = File.Create(topath + theEntry.Name))
??????????????????????????? {

??????????????????????????????? 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;
??????????????????????????????????? }
??????????????????????????????? }
??????????????????????????? }
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? return 1;
??????????? }
??????????? catch
??????????? {
??????????????? return 0;
??????????? }
??????? }
??? }
}

總結(jié)

以上是生活随笔為你收集整理的SharpZipLib压缩解压的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。