如何壓縮多個檔案/資料夾(GZipStream and C#)
publicclass GZip
{
///<summary>/// Compress
///</summary>///<param name="lpSourceFolder">The location of the files to include in the zip file, all files including files in subfolders will be included.</param>///<param name="lpDestFolder">Folder to write the zip file into
{
return Compress(lpSourceFolder, "*.*", SearchOption.AllDirectories, lpDestFolder, zipFileName,
}
///<summary>/// Compress
///</summary>///<param name="lpSourceFolder">The location of the files to include in the zip file</param>///<param name="searchPattern">Search pattern (ie "*.*" or "*.txt" or "*.gif") to idendify what files in lpSourceFolder to include in the zip file
{
DirectoryInfo di =new DirectoryInfo(lpSourceFolder);
FileInfo[] files = di.GetFiles("*.*", searchOption);
return Compress(files, lpSourceFolder, lpDestFolder, zipFileName, deleteTempFile);
}
///<summary>/// Compress
///</summary>///<param name="files">Array of FileInfo objects to be included in the zip file</param>///<param name="folders">Array of Folder string</param>///<param name="lpBaseFolder">Base folder to use when creating relative paths for the files
/// stored in the zip file. For example, if lpBaseFolder is 'C:\zipTest\Files\', and there is a file
/// 'C:\zipTest\Files\folder1\sample.txt' in the 'files' array, the relative path for sample.txt
/// will be 'folder1/sample.txt'</param>///<param name="lpDestFolder">Folder to write the zip file into</param>///<param name="zipFileName">Name of the zip file to write</param>publicstatic GZipResult Compress(FileInfo[] files, string[] folders, string lpBaseFolder, string lpDestFolder, string zipFileName)
{
//support compress folder IList<FileInfo> list =new List<FileInfo>();
foreach (FileInfo li in files)
list.Add(li);
foreach (string str in folders)
{
DirectoryInfo di =new DirectoryInfo(str);
foreach (FileInfo info in di.GetFiles("*.*", SearchOption.AllDirectories))
{
list.Add(info);
}
}
return Compress(list.ToArray(), lpBaseFolder, lpDestFolder, zipFileName, true);
}
///<summary>/// Compress
///</summary>///<param name="files">Array of FileInfo objects to be included in the zip file</param>///<param name="lpBaseFolder">Base folder to use when creating relative paths for the files
/// stored in the zip file. For example, if lpBaseFolder is 'C:\zipTest\Files\', and there is a file
/// 'C:\zipTest\Files\folder1\sample.txt' in the 'files' array, the relative path for sample.txt
/// will be 'folder1/sample.txt'</param>///<param name="lpDestFolder">Folder to write the zip file into</param>///<param name="zipFileName">Name of the zip file to write</param>publicstatic GZipResult Compress(FileInfo[] files, string lpBaseFolder, string lpDestFolder, string zipFileName)
{
return Compress(files, lpBaseFolder, lpDestFolder, zipFileName, true);
}
///<summary>/// Compress
///</summary>///<param name="files">Array of FileInfo objects to be included in the zip file</param>///<param name="lpBaseFolder">Base folder to use when creating relative paths for the files
/// stored in the zip file. For example, if lpBaseFolder is 'C:\zipTest\Files\', and there is a file
/// 'C:\zipTest\Files\folder1\sample.txt' in the 'files' array, the relative path for sample.txt
/// will be 'folder1/sample.txt'</param>///<param name="lpDestFolder">Folder to write the zip file into</param>///<param name="zipFileName">Name of the zip file to write</param>///<param name="deleteTempFile">Boolean, true deleted the intermediate temp file, false leaves the temp file in lpDestFolder (for debugging)</param>publicstatic GZipResult Compress(FileInfo[] files, string lpBaseFolder, string lpDestFolder, string zipFileName, bool deleteTempFile)
{
GZipResult result =new GZipResult();
try
{
if (!lpDestFolder.EndsWith("\\"))
{
lpDestFolder +="\\";
}
string lpTempFile = lpDestFolder + zipFileName +".tmp";
string lpZipFile = lpDestFolder + zipFileName;
result.TempFile = lpTempFile;
result.ZipFile = lpZipFile;
if (files !=null&& files.Length >0)
{
CreateTempFile(files, lpBaseFolder, lpTempFile, result);
if (result.FileCount >0)
{
CreateZipFile(lpTempFile, lpZipFile, result);
}
// delete the temp fileif (deleteTempFile)
{
File.Delete(lpTempFile);
result.TempFileDeleted =true;
}
}
}
catch//(Exception ex4) {
result.Errors =true;
}
return result;
}
privatestaticvoid CreateZipFile(string lpSourceFile, string lpZipFile, GZipResult result)
{
byte[] buffer;
int count =0;
FileStream fsOut =null;
FileStream fsIn =null;
GZipStream gzip =null;
// compress the file into the zip filetry
{
fsOut =new FileStream(lpZipFile, FileMode.Create, FileAccess.Write, FileShare.None);
gzip =new GZipStream(fsOut, CompressionMode.Compress, true);
fsIn =new FileStream(lpSourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer =newbyte[fsIn.Length];
count = fsIn.Read(buffer, 0, buffer.Length);
fsIn.Close();
fsIn =null;
// compress to the zip file gzip.Write(buffer, 0, buffer.Length);
result.ZipFileSize = fsOut.Length;
result.CompressionPercent = GetCompressionPercent(result.TempFileSize, result.ZipFileSize);
}
catch//(Exception ex1) {
result.Errors =true;
}
finally
{
if (gzip !=null)
{
gzip.Close();
gzip =null;
}
if (fsOut !=null)
{
fsOut.Close();
fsOut =null;
}
if (fsIn !=null)
{
fsIn.Close();
fsIn =null;
}
}
}
privatestaticvoid CreateTempFile(FileInfo[] files, string lpBaseFolder, string lpTempFile, GZipResult result)
{
byte[] buffer;
int count =0;
byte[] header;
string fileHeader =null;
string fileModDate =null;
string lpFolder =null;
int fileIndex =0;
string lpSourceFile =null;
string vpSourceFile =null;
GZipFileInfo gzf =null;
FileStream fsOut =null;
FileStream fsIn =null;
if (files !=null&& files.Length >0)
{
try
{
result.Files =new GZipFileInfo[files.Length];
// open the temp file for writing fsOut =new FileStream(lpTempFile, FileMode.Create, FileAccess.Write, FileShare.None);
foreach (FileInfo fi in files)
{
lpFolder = fi.DirectoryName +"\\";
try
{
gzf =new GZipFileInfo();
gzf.Index = fileIndex;
// read the source file, get its virtual path within the source folder lpSourceFile = fi.FullName;
gzf.LocalPath = lpSourceFile;
vpSourceFile = lpSourceFile.Replace(lpBaseFolder, string.Empty);
vpSourceFile = vpSourceFile.Replace("\\", "/");
gzf.RelativePath = vpSourceFile;
fsIn =new FileStream(lpSourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
buffer =newbyte[fsIn.Length];
count = fsIn.Read(buffer, 0, buffer.Length);
fsIn.Close();
fsIn =null;
fileModDate = fi.LastWriteTimeUtc.ToString();
gzf.ModifiedDate = fi.LastWriteTimeUtc;
gzf.Length = buffer.Length;
fileHeader = fileIndex.ToString() +","+ vpSourceFile +","+ fileModDate +","+ buffer.Length.ToString() +"\n";
header = Encoding.Default.GetBytes(fileHeader);
fsOut.Write(header, 0, header.Length);
fsOut.Write(buffer, 0, buffer.Length);
fsOut.WriteByte(10); // linefeed
gzf.AddedToTempFile =true;
// update the result object result.Files[fileIndex] = gzf;
// increment the fileIndex fileIndex++;
}
catch//(Exception ex1) {
result.Errors =true;
}
finally
{
if (fsIn !=null)
{
fsIn.Close();
fsIn =null;
}
}
if (fsOut !=null)
{
result.TempFileSize = fsOut.Length;
}
}
}
catch//(Exception ex2) {
result.Errors =true;
}
finally
{
if (fsOut !=null)
{
fsOut.Close();
fsOut =null;
}
}
}
result.FileCount = fileIndex;
}
publicstatic GZipResult Decompress(string lpSourceFolder, string lpDestFolder, string zipFileName)
{
return Decompress(lpSourceFolder, lpDestFolder, zipFileName, true, true, null, null, 4096);
}
publicstatic GZipResult Decompress(string lpSourceFolder, string lpDestFolder, string zipFileName, bool writeFiles, string addExtension)
{
return Decompress(lpSourceFolder, lpDestFolder, zipFileName, true, writeFiles, addExtension, null, 4096);
}
publicstatic GZipResult Decompress(string lpSrcFolder, string lpDestFolder, string zipFileName,
bool deleteTempFile, bool writeFiles, string addExtension, Hashtable htFiles, int bufferSize)
{
GZipResult result =new GZipResult();
if (!lpSrcFolder.EndsWith("\\"))
{
lpSrcFolder +="\\";
}
if (!lpDestFolder.EndsWith("\\"))
{
lpDestFolder +="\\";
}
string lpTempFile = lpSrcFolder + zipFileName +".tmp";
string lpZipFile = lpSrcFolder + zipFileName;
result.TempFile = lpTempFile;
result.ZipFile = lpZipFile;
string line =null;
string lpFilePath =null;
string lpFolder =null;
GZipFileInfo gzf =null;
FileStream fsTemp =null;
ArrayList gzfs =new ArrayList();
bool write =false;
if (string.IsNullOrEmpty(addExtension))
{
addExtension
相關推薦
如何壓縮多個檔案/資料夾(GZipStream and C#)
publicclass GZip { ///<summary>/// Compress ///</summary>///<param name="lpSourceFolder">The location of the f
centos 6.3 如何修改/etc/samba/smb.conf 檔案設定多個共享資料夾,一些是公開的,一些是需要認證的
第一節、samba是幹什麼的?它有什麼用? Samba(SMB是其縮寫) 是一個網路伺服器,它是Linux作為本地伺服器最重要的一個服務,用於Linux和Windows共享檔案之用;Samba可以用於Windows和Linux之間的共享檔案,也一樣用於Linux和Linux之間的共享檔案;不過對於L
MATLAB:如何在指定路徑下,讀取單個(多個)資料夾中所有影象
0. 選擇資料夾路徑: [filename filepath]=uigetfile('*.*','請選擇檔案');%filename為檔名,filepath為檔案路徑 image = imread(strcat(file_path,image_name));%讀取圖片檔案 1,
ZIP——記憶體壓縮多個檔案
using ICSharpCode.SharpZipLib.Zip; namespace MindExportLearning.Util { /// <summary> /// 壓縮和解壓檔案 /// </summary>
使用minizip解壓縮多個檔案(基於zlib)
寫作目的:之前在網上看到很多人在尋找可以解壓縮多個檔案的程式,其中有嘗試zlib的,使用zlib的原始碼可以生成字尾為點gz的壓縮檔案,但是一次只能壓縮一個檔案,無法壓縮多個檔案。其實,zlib的原始碼包裡有一個第三方的contrib已經實現解壓縮多個檔案,這就
VSCode開啟多個專案資料夾的解決方法
最近從sublime轉vscode,自然而然就會把sublime的一些習慣帶過來,其中有一點讓人頭疼的是: 當把一個資料夾拖進vscode裡面的時候,會把原來的資料夾覆蓋掉,這就意味著不能同時在vscode中開啟多個資料夾,用過sublime的同學都知道直接把資料夾拖進去就
java 壓縮/解壓 zip 多個檔案和資料夾
參考了幾篇文章,基本都是壓縮單個檔案或者一個資料夾,不能混合壓縮。 找了一個不需要額外jar包的程式碼上改的。(參考文章) 不需要額外jar包。 壓縮方法: import java.io.File; import java.io.FileInputStr
spark讀取多個資料夾(巢狀)下的多個檔案
在正常呼叫過程中,難免需要對多個資料夾下的多個檔案進行讀取,然而之前只是明確了Spark具備讀取多個檔案的能力。 針對多個資料夾下的多個檔案,以前的做法是先進行資料夾的遍歷,然後再進行各個資料夾目錄的讀取。 今天在做測試的時候,居然發現spark原生就支援這樣的能力。
python 讀取資料夾下多個檔案
import os os.chdir("G:\head in python\hfpy_ch5_data") L=[] for files in os.walk("G:\head in python\hfpy_ch5_data"): for file i
合併一個資料夾下多個檔案內容的單行shell命令
轉載網址:http://www.shangxueba.com/jingyan/1898710.html 合併一個資料夾下多個檔案內容: 複製程式碼程式碼如下: find -name "*.log" -exec 'cat' {} \; > test.txt
python實現將多個檔案分配到多個資料夾
import os import shutil #path of imgr path = 'D:\\BaiduNetdiskDownload\\newim\\' #path of folder folderPath = 'D:\\BaiduNetdiskDo
MFC對話方塊選擇多個檔案及選擇資料夾
選擇多個檔案(這裡選擇多張圖片) void SelctFiles() { CFileDialog dlg(TRUE, _T("*.jpg"), NULL, OFN_ALLOWMULTISE
MFC 利用CFileDialog讀取資料夾和多個檔案
專案1、彈出選擇視窗,選擇並獲得了多個檔案的檔名(不含路徑) void CMFrame::OnBnClickedDlginput() { // TODO: 在此新增控制元件通知處理程式程式碼 CFileDialog dlg( TRUE, NULL, NULL,
java 合併資料夾下多個檔案
package test.com.whty.platform.modules.interfaces; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im
input 選取資料夾及多個檔案 識別內部檔案
input 選取資料夾 input 檔案的選取 <input type="file" class="upfile" multiple="multiple" /> 可以選取多個檔案 <input type="fi
java實現多個檔案以壓縮包匯出到本地
描述:使用java將多個檔案同時壓縮為壓縮包,並匯出到本地 /** *壓縮檔案並匯出 */ public static void zipFiles() throws IOException { File file = null; String zipFileName = ""; File[
vue上傳多個檔案,附件和其他資料一起傳給後臺
前端: 實現多圖上傳主要用到以下兩個屬性: <el-form-item label="附件上傳" label-width="80px"> <el-f
C#合併多個包含資料的TXT檔案到指定XLSX檔案
該Demo實現將多個TXT檔案合併到同一個XLSX檔案同一個SHEET中,相應的對TXT檔案的格式也是有要求的,主要針對需要做資料統計的XLSX檔案,要求TXT檔案中的列相鄰資料之間有明確且統一的分離標識字元,每行資料的列數和列標題個數對應 說明:.NET F
php 將多個檔案壓縮成zip並下載到本地
廢話不多說,直接上程式碼 //這裡需要注意該目錄是否存在,並且有建立的許可權 $zipname = 'path/test.zip' //這是要打包的檔案地址陣列 $files = array("
java web 實現多個檔案壓縮下載
檔案下載時,我們可能需要一次下載多個檔案。批量下載檔案時,需要將多個檔案打包為zip,然後再下載。實現思路有兩種:一是將所有檔案先打包壓縮為一個檔案,然後下載這個壓縮包,二是一邊壓縮一邊下載,將多個檔案逐一寫入到壓縮檔案中。我這裡實現了邊壓縮邊下載。 下載樣式: 點選下載