AesZipFileEncrypter zipAndEncrypt method adds all folder tree to file

谁都会走 提交于 2019-12-08 05:09:48

问题


I'm using this method to zip and decrypt a file: AesZipFileEncrypter.zipAndEncrypt

This code:

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

compresses also the folder tree of my file, not just the file. For example: Adding C:\Test\Folder\MyFile.txt in the created zip file I will find the folders C:\Test\Folder\MyFile.txt also if I would like to have just MyFile.txt in the root folder. Is it possibile?


回答1:


This is the solution:

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



回答2:


In Windows (which it looks like you are in) I ran into the same problem and it seems to depend on where the file is relative to your application. To get around it, I copied the input file to the local directory, ziped and encrypted it, then moved the output file back to the where the output file was intended.

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;
}


来源:https://stackoverflow.com/questions/16399521/aeszipfileencrypter-zipandencrypt-method-adds-all-folder-tree-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!