ZipOutputStream
只能將文件夾中的文件壓縮。我也想壓縮子文件夾。我怎樣才能做到這一點?使用ZipOutputStream的Zip子文件夾
5
A
回答
7
您必須遞歸探索您的目錄才能在zip中添加所有文件。
見,如果你想這個小幫手:
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.IO;
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
namespace Zip
{
/// <summary>
/// Uses Sharpziplib so as to create a non flat zip archive
/// </summary>
public abstract class ZipManager
{
/// <summary>
/// will zip directory .\toto as .\toto.zip
/// </summary>
/// <param name="stDirToZip"></param>
/// <returns></returns>
public static string CreateZip(string stDirToZip)
{
try
{
DirectoryInfo di = new DirectoryInfo(stDirToZip);
string stZipPath = di.Parent.FullName + "\\" + di.Name + ".zip";
CreateZip(stZipPath, stDirToZip);
return stZipPath;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Main method
/// </summary>
/// <param name="stZipPath">path of the archive wanted</param>
/// <param name="stDirToZip">path of the directory we want to create, without ending backslash</param>
public static void CreateZip(string stZipPath, string stDirToZip)
{
try
{
//Sanitize inputs
stDirToZip = Path.GetFullPath(stDirToZip);
stZipPath = Path.GetFullPath(stZipPath);
Console.WriteLine("Zip directory " + stDirToZip);
//Recursively parse the directory to zip
Stack<FileInfo> stackFiles = DirExplore(stDirToZip);
ZipOutputStream zipOutput = null;
if (File.Exists(stZipPath))
File.Delete(stZipPath);
Crc32 crc = new Crc32();
zipOutput = new ZipOutputStream(File.Create(stZipPath));
zipOutput.SetLevel(6); // 0 - store only to 9 - means best compression
Console.WriteLine(stackFiles.Count + " files to zip.\n");
int index = 0;
foreach (FileInfo fi in stackFiles)
{
++index;
int percent = (int)((float)index/((float)stackFiles.Count/100));
if (percent % 1 == 0)
{
Console.CursorLeft = 0;
Console.Write(_stSchon[index % _stSchon.Length].ToString() + " " + percent + "% done.");
}
FileStream fs = File.OpenRead(fi.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
//Create the right arborescence within the archive
string stFileName = fi.FullName.Remove(0, stDirToZip.Length + 1);
ZipEntry entry = new ZipEntry(stFileName);
entry.DateTime = DateTime.Now;
// set Size and the crc, because the information
// about the size and crc should be stored in the header
// if it is not set it is automatically written in the footer.
// (in this case size == crc == -1 in the header)
// Some ZIP programs have problems with zip files that don't store
// the size and crc in the header.
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipOutput.PutNextEntry(entry);
zipOutput.Write(buffer, 0, buffer.Length);
}
zipOutput.Finish();
zipOutput.Close();
zipOutput = null;
}
catch (Exception)
{
throw;
}
}
static private Stack<FileInfo> DirExplore(string stSrcDirPath)
{
try
{
Stack<DirectoryInfo> stackDirs = new Stack<DirectoryInfo>();
Stack<FileInfo> stackPaths = new Stack<FileInfo>();
DirectoryInfo dd = new DirectoryInfo(Path.GetFullPath(stSrcDirPath));
stackDirs.Push(dd);
while (stackDirs.Count > 0)
{
DirectoryInfo currentDir = (DirectoryInfo)stackDirs.Pop();
try
{
//Process .\files
foreach (FileInfo fileInfo in currentDir.GetFiles())
{
stackPaths.Push(fileInfo);
}
//Process Subdirectories
foreach (DirectoryInfo diNext in currentDir.GetDirectories())
stackDirs.Push(diNext);
}
catch (Exception)
{//Might be a system directory
}
}
return stackPaths;
}
catch (Exception)
{
throw;
}
}
private static char[] _stSchon = new char[] { '-', '\\', '|', '/' };
}
}
2
本文由C#MVP彼得布朗伯格可以解決你的問題:Zip/Unzip folders and files with C#。它有完整的代碼和一個示例項目。
0
替代(和更方便的)解決方案,我只是偶然發現:使用SevenZipSharp項目
var compressor = new SevenZipCompressor();
compressor.ArchiveFormat = OutArchiveFormat.SevenZip;
compressor.CompressionLevel = CompressionLevel.High;
compressor.CompressionMode = CompressionMode.Create;
compressor.PreserveDirectoryRoot = false;
compressor.FastCompression = true;
compressor.CompressDirectory(dir.FullName, zipFile.FullName);
3
public static void ZipDirectoryKeepRelativeSubfolder(string zipFilePath, string directoryToZip)
{
var filenames = Directory.GetFiles(directoryToZip, "*.*", SearchOption.AllDirectories);
using (var s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9);// 0 - store only to 9 - means best compression
var buffer = new byte[4096];
foreach (var file in filenames)
{
var relativePath = file.Substring(directoryToZip.Length).TrimStart('\\');
var entry = new ZipEntry(relativePath);
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (var fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
相關問題
- 1. 子目錄Zip文件使用ZipOutputStream
- 2. 從其中有其他.zip文件的文件夾生成zipOutputstream
- 3. 壓縮(zip)使用ZipOutPutStream的文件列表Java
- 4. zip文件夾使用applescript
- 5. 專用子文件夾的zip目錄
- 6. 在Android中使用ZipOutputStream將最多24MB文件壓縮爲.zip
- 7. 從ZipOutPutStream使用Jax-rs下載Zip文件
- 8. 使用WinZip在子文件夾中的Zip文件
- 9. 使用Powershell編輯子文件夾中的zip文件內容
- 10. 使用ZipArchive的NSDocumentDirectory中的Zip子目錄/子文件夾
- 11. 有文件和文件夾使用ZIP
- 12. ZipFile使用zip中的所有文件夾創建zip文件
- 13. ZIP文件夾
- 14. 在Python中的子文件夾的ZIP文件夾
- 15. 使用Visual Basic 2010的Zip文件夾
- 16. iOS zip子目錄文件夾
- 17. 提取.zip到%appdata%\子文件夾
- 18. 使用php下載zip文件夾
- 19. ZipOutputStream在Android上生成損壞的zip文件
- 20. 如何在使用ZipOutputStream的zip文件中添加EmptyFolder或目錄
- 21. 在zip中使用ICSharpCode.SharpZipLib.Zip子文件夾時,它不應該是
- 22. 如何添加文件夾,子文件夾內現有的zip文件
- 23. ZIP文件中Bundle子文件夾的正確名稱格式?
- 24. 在PHP中提取ZIP文件的子文件夾
- 25. zip文件 - 排除的文件夾
- 26. 提取文件夾中的Zip文件
- 27. 如何使用VBScript將文件夾的每個子文件夾壓縮爲ZIP壓縮文件?
- 28. 使用壓縮文件的名稱命名Zip文件夾
- 29. 使用Python zipfile模塊刪除zip文件中的文件夾
- 30. 使用zipmodule重命名ZIP文件夾中的文件
我希望你沒有拋棄這個問題,將回到紀念答案
或至少指出哪些答案是有用的。在你的200多個問題中,你將有大約120個需要答案的標記(如果答案令人滿意),每個標記的答案將爲你贏得額外的2個代表點,並幫助你的問題的未來觀衆知道最佳答案是什麼。 120 * 2rep = 240代表你可以獲得。 – slugster 2012-06-09 03:34:53