iT邦幫忙

1

c# 解壓縮方法

客戶固定會寄email給我,內容是一個zip檔。之前都是乖乖的把附件存出來,然後用7z解壓縮,key密碼,存到固定的資料夾。
現在想自已寫一支小程式把這個動作自動化,但卡在解壓縮這一段。我試過用 DotNetZip,發現解出來的檔案名稱會變成亂碼:
http://ithelp.ithome.com.tw/upload/images/20170602/20017351RUK5UkIdLV.png

底下是程式碼

                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);
                }
weiclin iT邦高手 4 級 ‧ 2017-06-02 15:37:42 檢舉
用程式去呼叫 7z 解壓
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
海綿寶寶
iT邦大神 1 級 ‧ 2017-06-02 12:34:51
最佳解答
Brandon iT邦新手 4 級 ‧ 2017-06-02 14:18:14 檢舉

謝謝回覆。不過他說的是產生zip檔的處理,我的問題是解壓時的處理。順便說一句,原始檔我用winrar與7z 開,檔名都是正常的。

設個ReadOption看看

Brandon iT邦新手 4 級 ‧ 2017-06-02 18:22:57 檢舉

正解!/images/emoticon/emoticon12.gif

0
aabbb
iT邦新手 5 級 ‧ 2017-06-02 14:35:30

試試用最新的7 ZIP解壓,現在出到VERSION 16

Brandon iT邦新手 4 級 ‧ 2017-06-02 14:48:30 檢舉

用 7z 解壓是沒問題的,但我是要在程式裡面解壓。

0
Daniel
iT邦新手 2 級 ‧ 2017-06-02 15:51:14

如果你只需要解壓縮功能,那麼下載 "7z<版號>-extra.7z",解開後試試看在 C# 呼叫 7za.exe 處理,或者下載 7zip 提供的 SDK 研究。不過我想用一個批次檔應該就能解決你的問題了。

我要發表回答

立即登入回答