AesZipFileEncrypter zipAndEncrypt method adds all

2019-06-08 09:41发布

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?

2条回答
啃猪蹄的小仙女
2楼-- · 2019-06-08 10:21

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;
}
查看更多
姐就是有狂的资本
3楼-- · 2019-06-08 10:43

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();
查看更多
登录 后发表回答