2015-03-13 71 views
-2

我已經使用ionic.zip.dll如何通過使用c#以寫模式打開密碼保護的Zip文件?

看到下面的代碼

using (ZipFile zip = new ZipFile()) 
{ 
    TargetFileName = TargetNamePrefix; 
    OutPutDirectoryPath = TargetLocation + "\\" + TargetNamePrefix + ".zip";     

    zip.Password = zipFilePassword; 
    zip.AddFiles(FilePaths, TargetNamePrefix); 
    zip.Save(OutPutDirectoryPath) 
} 

此文件路徑保護帶拉鍊的CSV文件,是字符串[]變量它包括文件(CSV /文本)名稱。 TargetNamePrefix表示在zipfile文件夾內Name.OutPutDirectoryPath表示 用zipfileName輸出directoty。 那麼我怎樣才能以寫模式打開那些受保護的文件,爲什麼因爲我想寫Datainto受保護的csv文件。

+0

可能重複與.NET 4.5](http://stackoverflow.com/questions/13160490/decompressing-password-protected-zip-files-with-net-4-5) – BCdotWEB 2015-03-13 12:29:09

回答

0

您可以使用類似的方法,只是提取出來:

using(ZipFile zip = ZipFile.Read(OutPutDirectoryPath)) 
{ 
    zip.Password = zipFilePassword; 
    try 
    { 
     zip.ExtractAll(TargetLocation); 
    } 
    catch (BadPasswordException e) 
    { 
     // handle error 
    } 
} 

更新 訪問單個文件進入流,而不提取[解壓縮密碼保護的ZIP文件

string entry_name = TargetNamePrefix + ".csv"; // update this if needed 
using (ZipFile zip = ZipFile.Read(OutPutDirectoryPath)) 
{ 
    // Set password for file 
    zip.Password = zipFilePassword; 

    // Extract entry into a memory stream 
    ZipEntry e = zip[entry_name]; 
    using(MemoryStream m = new MemoryStream()) 
    { 
     e.Extract(m); 
     // Update m stream 
    } 

    // Remove old entry 
    zip.RemoveEntry(entry_name); 

    // Add new entry 
    zip.AddEntry(entry_name, m); 

    // Save 
    zip.Save(OutPutDirectoryPath); 
} 
+0

對不起...我對提取不感興趣,因爲我要爲了保護外部世界的數據,只需要在寫入模式下從ZIP文件中打開所需的文件請幫我解開這個.... – Pavan 2015-03-16 05:15:46

+0

@Pavan - 查看更新,讓我知道如果這對你有用。 – SwDevMan81 2015-03-16 12:29:33

+0

Hi..Good Evening, – Pavan 2015-03-18 12:45:04

相關問題