java如何在没有keymanager的情况下使用TrueVFS(was TrueZip)ZipoutStream创建密码保护的Zip流?
import net.java.truevfs.comp.zip.ZipOutputStream;
...
ZipOutputStream zos = new ZipOutputStream(outPipe);
zos.setCryptoParameters( ## How to create those Crypto Parameters ### );
密码参数必须是接口ZipCryptoParameters
。类KeyManagerZipCryptoParameters
正在实现这一点——但我不想要密钥管理器,我只想简单地硬编码一个特定的密码
我该怎么做
编辑
我试过了
private static final class CustomWinZipAesParameters
implements WinZipAesParameters {
final byte[] password;
CustomWinZipAesParameters(final byte[] password) {
this.password = password.clone();
}
@Override
public byte[] getWritePassword(String name)
throws ZipKeyException {
return password.clone();
}
@Override
public byte[] getReadPassword(String name, boolean invalid)
throws ZipKeyException {
if (invalid)
throw new ZipKeyException(name + " (invalid password)");
return password.clone();
}
@Override
public AesKeyStrength getKeyStrength(String arg0)
throws ZipKeyException {
return AesKeyStrength.BITS_128;
}
@Override
public void setKeyStrength(String name, AesKeyStrength keyStrength)
throws ZipKeyException {
// We have been using only 128 bits to create archive entries.
assert AesKeyStrength.BITS_128 == keyStrength;
}
} // CustomWinZipAesParameters
然后
byte[] ba = {64, 64, 64, 64, 64};
zos.setCryptoParameters(new CustomWinZipAesParameters(ba));
但是产生的ZIP文件并没有被加密
# 1 楼答案
我只需要设置每个文件的加密标志。(除了我上面的编辑)。然后它就起作用了
注意:我只能使用
7z
提取文件,而不能在Linux上使用unzip
命令(否则我会得到“不支持的压缩方法99”)