iT邦幫忙

0

asp.net SharpZip 加密

各位先進好:
想請教網頁檔案下載加密的問題。
我有做一個多檔壓縮下載的功能,是用SharpZip打包成壓縮檔的,下載完後想要在壓縮檔上加密。
zip.Password = "1234";
但加密後,是針對壓縮檔裡的每個檔案做加密,而不是對壓縮檔加密。<就是每個檔案打開都要輸入一次密碼>
不知道是否有可以對壓縮檔加密的方法。

另外線上匯出excel加密的部份,上網查了一下,好像如果要加密的話,不好實現,不知是否有其它方式可以提供參考的,謝謝。

2 個回答

2
丁丁 (Dean)
iT邦大師 6 級 ‧ 2013-09-26 11:04:33

leo226提到:
另外線上匯出excel加密的部份,上網查了一下,好像如果要加密的話,不好實現,不知是否有其它方式可以提供參考的,謝謝。

可以參考下篇利用 SmartXLS
C#如何匯出excel文件並加密?

leo226提到:
不知道是否有可以對壓縮檔加密的方法。

可以貼你的 Code 上來看嗎? 我整個目錄打包是只要輸入一次密碼... (一樣用SharpZip

<pre class="c" name="code">public void CreateZip(string outPathname, string password, string folderName) {

    FileStream fsOut = File.Create(outPathname);
    ZipOutputStream zipStream = new ZipOutputStream(fsOut);
    zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
    zipStream.Password = password;  // optional. Null is the same as not setting. Required if using AES.
    int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);
    CompressFolder(folderName, zipStream, folderOffset);
    zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
    zipStream.Close();
}

private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) {

    string[] files = Directory.GetFiles(path);

    foreach (string filename in files) {
        FileInfo fi = new FileInfo(filename);
        string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
        entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
        ZipEntry newEntry = new ZipEntry(entryName);
        newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity
        newEntry.Size = fi.Length;
        zipStream.PutNextEntry(newEntry);
        byte[ ] buffer = new byte[4096];
        using (FileStream streamReader = File.OpenRead(filename)) {
            StreamUtils.Copy(streamReader, zipStream, buffer);
        }
        zipStream.CloseEntry();
    }
    string[ ] folders = Directory.GetDirectories(path);
    foreach (string folder in folders) {
        CompressFolder(folder, zipStream, folderOffset);
    }
}

我要發表回答

立即登入回答