Java Code Examples for net.lingala.zip4j.model.LocalFileHeader#getAesExtraDataRecord()

The following examples show how to use net.lingala.zip4j.model.LocalFileHeader#getAesExtraDataRecord() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Zip4jUtil.java    From zip4j with Apache License 2.0 5 votes vote down vote up
public static CompressionMethod getCompressionMethod(LocalFileHeader localFileHeader) {
  if (localFileHeader.getCompressionMethod() != CompressionMethod.AES_INTERNAL_ONLY) {
    return localFileHeader.getCompressionMethod();
  }

  if (localFileHeader.getAesExtraDataRecord() == null) {
    throw new RuntimeException("AesExtraDataRecord not present in local header for aes encrypted data");
  }

  return localFileHeader.getAesExtraDataRecord().getCompressionMethod();
}
 
Example 2
Source File: AesCipherInputStream.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private byte[] getSalt(LocalFileHeader localFileHeader) throws IOException {
  if (localFileHeader.getAesExtraDataRecord() == null) {
    throw new IOException("invalid aes extra data record");
  }

  AESExtraDataRecord aesExtraDataRecord = localFileHeader.getAesExtraDataRecord();
  byte[] saltBytes = new byte[aesExtraDataRecord.getAesKeyStrength().getSaltLength()];
  readRaw(saltBytes);
  return saltBytes;
}
 
Example 3
Source File: CipherInputStream.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private CompressionMethod getCompressionMethod(LocalFileHeader localFileHeader) throws ZipException {
  if (localFileHeader.getCompressionMethod() != CompressionMethod.AES_INTERNAL_ONLY) {
    return localFileHeader.getCompressionMethod();
  }

  if (localFileHeader.getAesExtraDataRecord() == null) {
    throw new ZipException("AesExtraDataRecord not present in localheader for aes encrypted data");
  }

  return localFileHeader.getAesExtraDataRecord().getCompressionMethod();
}
 
Example 4
Source File: HeaderWriter.java    From zip4j with Apache License 2.0 4 votes vote down vote up
public void writeLocalFileHeader(ZipModel zipModel, LocalFileHeader localFileHeader, OutputStream outputStream,
                                 Charset charset) throws IOException {

  try(ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
    rawIO.writeIntLittleEndian(byteArrayOutputStream, (int) localFileHeader.getSignature().getValue());
    rawIO.writeShortLittleEndian(byteArrayOutputStream, localFileHeader.getVersionNeededToExtract());
    byteArrayOutputStream.write(localFileHeader.getGeneralPurposeFlag());
    rawIO.writeShortLittleEndian(byteArrayOutputStream, localFileHeader.getCompressionMethod().getCode());

    rawIO.writeLongLittleEndian(longBuff, 0, localFileHeader.getLastModifiedTime());
    byteArrayOutputStream.write(longBuff, 0, 4);

    rawIO.writeLongLittleEndian(longBuff, 0, localFileHeader.getCrc());
    byteArrayOutputStream.write(longBuff, 0, 4);

    boolean writeZip64Header = localFileHeader.getCompressedSize() >= ZIP_64_SIZE_LIMIT
        || localFileHeader.getUncompressedSize() >= ZIP_64_SIZE_LIMIT;

    if (writeZip64Header) {
      rawIO.writeLongLittleEndian(longBuff, 0, ZIP_64_SIZE_LIMIT);

      //Set the uncompressed size to ZipConstants.ZIP_64_SIZE_LIMIT as
      //these values will be stored in Zip64 extra record
      byteArrayOutputStream.write(longBuff, 0, 4);
      byteArrayOutputStream.write(longBuff, 0, 4);

      zipModel.setZip64Format(true);
      localFileHeader.setWriteCompressedSizeInZip64ExtraRecord(true);
    } else {
      rawIO.writeLongLittleEndian(longBuff, 0, localFileHeader.getCompressedSize());
      byteArrayOutputStream.write(longBuff, 0, 4);

      rawIO.writeLongLittleEndian(longBuff, 0, localFileHeader.getUncompressedSize());
      byteArrayOutputStream.write(longBuff, 0, 4);

      localFileHeader.setWriteCompressedSizeInZip64ExtraRecord(false);
    }

    byte[] fileNameBytes = new byte[0];
    if (isStringNotNullAndNotEmpty(localFileHeader.getFileName())) {
      fileNameBytes = localFileHeader.getFileName().getBytes(charset);
    }
    rawIO.writeShortLittleEndian(byteArrayOutputStream, fileNameBytes.length);

    int extraFieldLength = 0;
    if (writeZip64Header) {
      extraFieldLength += ZIP64_EXTRA_DATA_RECORD_SIZE_LFH + 4; // 4 for signature + size of record
    }
    if (localFileHeader.getAesExtraDataRecord() != null) {
      extraFieldLength += AES_EXTRA_DATA_RECORD_SIZE;
    }
    rawIO.writeShortLittleEndian(byteArrayOutputStream, extraFieldLength);

    if (fileNameBytes.length > 0) {
      byteArrayOutputStream.write(fileNameBytes);
    }

    //Zip64 should be the first extra data record that should be written
    //This is NOT according to any specification but if this is changed
    //corresponding logic for updateLocalFileHeader for compressed size
    //has to be modified as well
    if (writeZip64Header) {
      rawIO.writeShortLittleEndian(byteArrayOutputStream,
          (int) HeaderSignature.ZIP64_EXTRA_FIELD_SIGNATURE.getValue());
      rawIO.writeShortLittleEndian(byteArrayOutputStream, ZIP64_EXTRA_DATA_RECORD_SIZE_LFH);
      rawIO.writeLongLittleEndian(byteArrayOutputStream, localFileHeader.getUncompressedSize());
      rawIO.writeLongLittleEndian(byteArrayOutputStream, localFileHeader.getCompressedSize());
    }

    if (localFileHeader.getAesExtraDataRecord() != null) {
      AESExtraDataRecord aesExtraDataRecord = localFileHeader.getAesExtraDataRecord();
      rawIO.writeShortLittleEndian(byteArrayOutputStream, (int) aesExtraDataRecord.getSignature().getValue());
      rawIO.writeShortLittleEndian(byteArrayOutputStream, aesExtraDataRecord.getDataSize());
      rawIO.writeShortLittleEndian(byteArrayOutputStream, aesExtraDataRecord.getAesVersion().getVersionNumber());
      byteArrayOutputStream.write(aesExtraDataRecord.getVendorID().getBytes());

      byte[] aesStrengthBytes = new byte[1];
      aesStrengthBytes[0] = (byte) aesExtraDataRecord.getAesKeyStrength().getRawCode();
      byteArrayOutputStream.write(aesStrengthBytes);

      rawIO.writeShortLittleEndian(byteArrayOutputStream, aesExtraDataRecord.getCompressionMethod().getCode());
    }

    outputStream.write(byteArrayOutputStream.toByteArray());
  }
}
 
Example 5
Source File: AesCipherInputStream.java    From zip4j with Apache License 2.0 4 votes vote down vote up
@Override
protected AESDecrypter initializeDecrypter(LocalFileHeader localFileHeader, char[] password) throws IOException {
  return new AESDecrypter(localFileHeader.getAesExtraDataRecord(), password, getSalt(localFileHeader), getPasswordVerifier());
}