如何使用TrueVFS创建受密码保护的Zip Stream(是TrueZip)ZipOutputStream而不使用keymanager?

时间:2016-10-15 13:49:04

标签: java zip truezip truevfs

import net.java.truevfs.comp.zip.ZipOutputStream;

...


ZipOutputStream zos = new ZipOutputStream(outPipe);
zos.setCryptoParameters( ## How to create those Crypto Parameters ### );

cryptoparameters必须是接口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 个答案:

答案 0 :(得分:0)

I simply that to set the encrypted flag per file. (in addition to my edit above). Then it works.

Beware: I can only extract the file with 7z - not with the unzip command on Linux (otherwise I get "unsupported compression method 99")

File file = new File(contentManager.getOsmAndFilePlain());

ZipEntry ze = new ZipEntry(ContentManager.PRO_APP_FILENAME);
ze.setEncrypted(true);   ## this line inserted
zos.putNextEntry(ze);