2013-05-06 76 views
1

我使用這種方法來壓縮和解密文件: AesZipFileEncrypter.zipAndEncryptAesZipFileEncrypter zipAndEncrypt方法添加的所有文件夾樹到文件

此代碼:

AesZipFileEncrypter.zipAndEncrypt(new File("C:\Test\Folder\MyFile.txt"), new File("C:\Test\Folder\MyZip.zip"), password, aesEncrypter); 

也壓縮我的文件的文件夾樹,不只是文件。例如: 在創建的zip文件中添加C:\ Test \ Folder \ MyFile.txt如果我想在根文件夾中只放入MyFile.txt,我會找到文件夾C:\ Test \ Folder \ MyFile.txt 。 它可能嗎?

回答

2

這是解決方案:

AESEncrypter aesEncrypter = new AESEncrypterBC(); 
    aesEncrypter.init(password, 0); 
    AesZipFileEncrypter ze=new AesZipFileEncrypter(outputfilename, aesEncrypter); 
    ze.add(inputfilename,new FileInputStream(inputfilename), password); 
    ze.close(); 
0

在Windows(它看起來像你在)我遇到了同樣的問題,它似乎取決於該文件是相對於您的應用程序。爲了解決它,我將輸入文件複製到本地目錄,ziped並加密它,然後將輸出文件移回到輸出文件的目標位置。

public static File aesEncrypt(String inFileFullPath, String outFileFullPath, String aesPassword) throws IOException{ 
    File inFile = new File(inFileFullPath); 
    File localInput = new File(inFile.getName()); 
    Files.copy(inFile, localInput); 

    File outFile = new File(outFileFullPath); 
    File localOutFile = new File(outFile.getName()); 

    AESEncrypter aesEncrypter = new AESEncrypterBC(); 
    aesEncrypter.init(aesPassword, 255); 
    AesZipFileEncrypter ze = new AesZipFileEncrypter(localOutFile, aesEncrypter); 
    ze.add(localInput, aesPassword); 
    ze.close(); 

    Files.move(localOutFile, outFile); 
    localInput.delete(); 
    return outFile; 
} 
+1

我的解決方案,你可以用一個文件得到周圍沒有應對它,但是變得容易,更多的文件,你需要管理一個循環 – Tobia 2014-01-24 08:12:36

+0

我用 字節[]數據= {1,2,3,4 ,5}; File outputZipFile = new File(「someZipName」); AesZipFileEncrypter ze = new AesZipFileEncrypter(outputZipFile,ENCRIPTER); ze.add(「name.zip」,新的ByteArrayInputStream(data),密碼); ze.add ze.close(); 但無效,它創建一個名爲「someZipName」的存檔,當我打開它時,您可以看到打開時產生錯誤的文件「name.zip」。 – marisxanis 2014-02-14 20:43:45

+0

只是爲了瞭解如何在使用DECRYPTER之後使用DECRYPTER,對於我使用的是unzip()方法;) 請參閱以下代碼: https://code.google.com/p/winzipaes/source/browse/trunk/winzipaes/src /de/idyl/crypto/zip/AesZipFileEncrypter.java?r=44 – marisxanis 2014-02-15 10:19:56

相關問題