.NET ez 2011-11-30
SevenZipSharp 下載:http://sevenzipsharp.codeplex.com/ 或 SevenZipSharp
using System;
using System.IO;
using System.Threading;
using SevenZip;
namespace Test
{
class Program
{
static void Main(string[] args)
{
string ApplicationBase = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//壓縮檔案
{
string zip_name = Path.Combine(ApplicationBase, "test.7z");
string dir_route = Path.Combine(ApplicationBase, "test");
SevenZipCompressor sz = new SevenZipCompressor();
sz.ArchiveFormat = OutArchiveFormat.SevenZip;
sz.CompressionLevel = CompressionLevel.Ultra;
sz.CompressDirectory(dir_route, zip_name);
}
//壓縮檔案並顯示進度
{
string zip_name = Path.Combine(ApplicationBase, "test1.7z");
string dir_route = Path.Combine(ApplicationBase, "test");
Thread t = new Thread(() =>
{
SevenZipCompressor tmp = new SevenZipCompressor();
tmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>((s, e) =>
{
Console.WriteLine(String.Format("[{0}%] {1}", e.PercentDone, e.FileName));
});
tmp.CompressionFinished += new EventHandler<EventArgs>((s, e) =>
{
Console.WriteLine("壓縮完成");
});
tmp.CompressDirectory(dir_route, zip_name);
});
t.Start();
t.Join();
}
//解壓縮檔案(全部)
{
string zip_name = Path.Combine(ApplicationBase, "test.7z");
string dir_route = Path.Combine(ApplicationBase, "test1");
using (SevenZipExtractor tmp = new SevenZipExtractor(zip_name))
{
tmp.ExtractArchive(dir_route);
}
}
//解壓縮檔案並顯示進度
{
string zip_name = Path.Combine(ApplicationBase, "test.7z");
string dir_route = Path.Combine(ApplicationBase, "test2");
Thread t = new Thread(() => {
using (SevenZipExtractor tmp = new SevenZipExtractor(zip_name))
{
tmp.FileExtractionStarted += new EventHandler<FileInfoEventArgs>((s, e) =>
{
Console.WriteLine(String.Format("[{0}%] {1}", e.PercentDone, e.FileInfo.FileName));
});
tmp.ExtractionFinished += new EventHandler<EventArgs>((s, e) =>
{
Console.WriteLine("完成解壓縮");
});
tmp.ExtractArchive(dir_route);
}
});
t.Start();
t.Join();
}
//列出壓縮檔內容
{
string zip_name = Path.Combine(ApplicationBase, "test.7z");
using (SevenZipExtractor tmp = new SevenZipExtractor(zip_name))
{
for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
{
Console.WriteLine(tmp.ArchiveFileData[i].FileName.ToString());
}
}
}
Console.ReadLine();
}
}
}
標籤: .NET
本文章網址:
https://www.ez2o.com/Blog/Post/csharp-SharpZipLib-sample-code-New
https://www.ez2o.com/Blog/Post/57
https://www.ez2o.com/Blog/Post/csharp-SharpZipLib-sample-code-New
https://www.ez2o.com/Blog/Post/57
