客戶固定會寄email給我,內容是一個zip檔。之前都是乖乖的把附件存出來,然後用7z解壓縮,key密碼,存到固定的資料夾。
現在想自已寫一支小程式把這個動作自動化,但卡在解壓縮這一段。我試過用 DotNetZip,發現解出來的檔案名稱會變成亂碼:
底下是程式碼
using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(src))
{
// This call to ExtractAll() assumes:
// - none of the entries are password-protected.
// - want to extract all entries to current working directory
// - none of the files in the zip already exist in the directory;
// if they do, the method will throw.
string sOutputPath = Path.Combine(Path.GetDirectoryName(src), Path.GetFileNameWithoutExtension(src));
//zip.Encryption = EncryptionAlgorithm.WinZipAes256;
zip.Password = "xxxxx";
zip.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
zip.FlattenFoldersOnExtract = true;
zip.AlternateEncoding = Encoding.GetEncoding("Big5");
zip.AlternateEncodingUsage = ZipOption.Always;
zip.ExtractAll(sOutputPath);
}
也試過用 sevenzipsharp, 但一直卡在
cannot load 7-zip library or internal com error
(試過google大神建議的各種方法,都無解)
請問各位有什麼建議嗎?
(程式碼寫錯,或換個解壓方法,都歡迎討論)
重貼一下最後的程式碼:
ReadOptions options = new ReadOptions();
options.Encoding = Encoding.Default;
using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(src,options))
{
string sOutputPath = Path.Combine(Path.GetDirectoryName(src), Path.GetFileNameWithoutExtension(src));
zip.Password = "XXXXXX";
zip.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
zip.FlattenFoldersOnExtract = true;
zip.ExtractAll(sOutputPath);
}
試試用最新的7 ZIP解壓,現在出到VERSION 16
如果你只需要解壓縮功能,那麼下載 "7z<版號>-extra.7z",解開後試試看在 C# 呼叫 7za.exe 處理,或者下載 7zip 提供的 SDK 研究。不過我想用一個批次檔應該就能解決你的問題了。