我有以下代码在Android上使用zip4j读取加密的zip文件。 我不提供临时文件。 zip4j 会创建一个用于解密的临时文件吗?还是zip标准允许即时解密,因此不会暂时将加密数据写入存储?
ZipFile table = null;
try {
table = new ZipFile("/sdcard/file.zip");
if( table.isEncrypted() ){
table.setPassword("password");
}
} catch (Exception e) {
// if can't be opened then return null
e.printStackTrace();
return;
}
InputStream in = null;
try {
FileHeader entry = table.getFileHeader("file.txt");
in = table.getInputStream(entry);
...
作者,我可以向你保证,Zip4j不会创建任何用于解密的临时文件。
Zip4j将解密内存中的数据,并且不会将加密数据写入任何临时文件。Zip 格式规范允许对 AES 和标准 Zip 加密进行动态或内存中解密。
来自zip4j源代码
public ZipInputStream getInputStream() throws ZipException {
if (fileHeader == null) {
throw new ZipException("file header is null, cannot get inputstream");
}
RandomAccessFile raf = null;
try {
raf = createFileHandler(InternalZipConstants.READ_MODE);
String errMsg = "local header and file header do not match";
//checkSplitFile();
if (!checkLocalHeader())
throw new ZipException(errMsg);
init(raf);
...
}
private RandomAccessFile createFileHandler(String mode) throws ZipException {
if (this.zipModel == null || !Zip4jUtil.isStringNotNullAndNotEmpty(this.zipModel.getZipFile())) {
throw new ZipException("input parameter is null in getFilePointer");
}
try {
RandomAccessFile raf = null;
if (zipModel.isSplitArchive()) {
raf = checkSplitFile();
} else {
raf = new RandomAccessFile(new File(this.zipModel.getZipFile()), mode);
}
return raf;
} catch (FileNotFoundException e) {
throw new ZipException(e);
} catch (Exception e) {
throw new ZipException(e);
}
}
我相信raf = new RandomAccessFile(new File(this.zipModel.getZipFile()), mode);
行意味着它确实在加密zip文件的路径子目录下制作解密文件。
我不知道你是否可以即时解压缩(可能不会(。如果您不希望人们查看解密的文件,请考虑将 zip 文件存储在应用的受保护的内部存储空间中,而不是 SD 卡中。