2016-02-23 26 views
-1

,因爲我可以在一個zip文件把密碼的Grails放密碼的壓縮文件中的Grails錯誤

package zip 
import java.util.zip.ZipEntry 
import java.util.zip.ZipOutputStream 

class SampleZipController { 
def index() { } 
def downloadSampleZip() { 
    response.setContentType('APPLICATION/OCTET-STREAM') 
    response.setHeader('Content-Disposition', 'Attachment;Filename="example.zip"') 
    ZipOutputStream zip = new ZipOutputStream(response.outputStream); 

    def file1Entry = new ZipEntry('first_file.txt'); 
    zip.putNextEntry(file1Entry); 
    zip.write("This is the content of the first file".bytes); 
    def file2Entry = new ZipEntry('second_file.txt'); 
    zip.putNextEntry(file2Entry); 
    zip.write("This is the content of the second file".bytes); 
    zip.setPassword("password"); 

    zip.close(); 

} 
} 

的問題是,我把setPassword財產,不會產生任何的壓縮文件案「 m保留錯誤的單詞。

+1

「setPassword()」方法記錄在哪裏?我沒有在Java文檔中看到它。 –

+0

謝謝你的迴應真的找不到文檔setPassword是正確的關鍵字。 –

回答

0

好的,這是交易。 Java SDK不提供zip文件的密碼保護。 ZipOutputStream.setPassword()不存在,也沒有任何其他方法來設置密碼。

你可以做的是使用第三方庫代替。這裏是一個Groovy腳本演示瞭如何用密碼保護使用Winzipaes一個ZIP文件:

@Grab('de.idyl:winzipaes:1.0.1') 

import de.idyl.winzipaes.AesZipFileEncrypter 
import de.idyl.winzipaes.impl.AESEncrypterBC 

def outputStream = new FileOutputStream('test.zip') /* Create an OutputStream */ 
def encrypter = new AesZipFileEncrypter(outputStream, new AESEncrypterBC()) 
def password = 'password' 

encrypter.add('first_file.txt', new ByteArrayInputStream('This is the content of the first file'.bytes), password) 
encrypter.add('second_file.txt', new ByteArrayInputStream('This is the content of the second file'.bytes), password) 
encrypter.close() 

正如你所看到的API類似,您使用的一個。請記住,這是一個Groovy腳本沒有 Grails,因此請確保適當添加Winzipaes依賴項(不要使用@Grab)。

注意:加密算法是AES256,所有ZIP客戶端都不支持該算法。例如,在Mac OSX上,我必須安裝7-zip解密文件;內置的ZIP工具不起作用。

你可以閱讀更多關於密碼保護ZIP文件here

+0

感謝您的回覆。修改代碼,以便.zip可以下載,但我不能這樣做可以幫助我。 –

+0

當然。我已經回答了問題,這樣就完成了。只需加入Grails Noob Slack社區,我可以幫你弄清楚剩下的部分。這就在這裏:http://emmanuelrosa.com –