Java Code Examples for net.lingala.zip4j.model.ZipParameters#isWriteExtendedLocalFileHeader()

The following examples show how to use net.lingala.zip4j.model.ZipParameters#isWriteExtendedLocalFileHeader() . 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: FileHeaderFactory.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private byte generateFirstGeneralPurposeByte(boolean isEncrypted, ZipParameters zipParameters) {

    byte firstByte = 0;

    if (isEncrypted) {
      firstByte = setBit(firstByte, 0);
    }

    if (CompressionMethod.DEFLATE.equals(zipParameters.getCompressionMethod())) {
      if (CompressionLevel.NORMAL.equals(zipParameters.getCompressionLevel())) {
        firstByte = unsetBit(firstByte, 1);
        firstByte = unsetBit(firstByte, 2);
      } else if (CompressionLevel.MAXIMUM.equals(zipParameters.getCompressionLevel())) {
        firstByte = setBit(firstByte, 1);
        firstByte = unsetBit(firstByte, 2);
      } else if (CompressionLevel.FAST.equals(zipParameters.getCompressionLevel())) {
        firstByte = unsetBit(firstByte, 1);
        firstByte = setBit(firstByte, 2);
      } else if (CompressionLevel.FASTEST.equals(zipParameters.getCompressionLevel())
          || CompressionLevel.ULTRA.equals(zipParameters.getCompressionLevel())) {
        firstByte = setBit(firstByte, 1);
        firstByte = setBit(firstByte, 2);
      }
    }

    if (zipParameters.isWriteExtendedLocalFileHeader()) {
      firstByte = setBit(firstByte, 3);
    }

    return firstByte;
  }
 
Example 2
Source File: ZipOutputStream.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyZipParameters(ZipParameters zipParameters) {
  if (zipParameters.getCompressionMethod() == CompressionMethod.STORE
      && zipParameters.getEntrySize() < 0
      && !isEntryDirectory(zipParameters.getFileNameInZip())
      && zipParameters.isWriteExtendedLocalFileHeader()) {
    throw new IllegalArgumentException("uncompressed size should be set for zip entries of compression type store");
  }
}
 
Example 3
Source File: ZipStandardCipherOutputStream.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private long getEncryptionKey(ZipParameters zipParameters) {
  if (zipParameters.isWriteExtendedLocalFileHeader()) {
    long dosTime = Zip4jUtil.javaToDosTime(zipParameters.getLastModifiedFileTime());
    return (dosTime & 0x0000ffff) << 16;
  }

  return zipParameters.getEntryCRC();
}
 
Example 4
Source File: FileHeaderFactoryTest.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyFileHeader(FileHeader fileHeader, ZipParameters zipParameters, boolean isSplitZip,
                              int diskNumberStart, int versionNeededToExtract, boolean aesExtraDataRecordPresent) {
  assertThat(fileHeader).isNotNull();
  assertThat(fileHeader.getSignature()).isEqualTo(HeaderSignature.CENTRAL_DIRECTORY);
  assertThat(fileHeader.getVersionMadeBy()).isEqualTo(819);
  assertThat(fileHeader.getVersionNeededToExtract()).isEqualTo(versionNeededToExtract);
  verifyCompressionMethod(fileHeader, zipParameters);
  assertThat(fileHeader.isEncrypted()).isEqualTo(zipParameters.isEncryptFiles());
  assertThat(fileHeader.getEncryptionMethod()).isEqualTo(zipParameters.isEncryptFiles()
      ? zipParameters.getEncryptionMethod() : EncryptionMethod.NONE);
  assertThat(fileHeader.getFileName()).isEqualTo(FILE_NAME_IN_ZIP);
  assertThat(fileHeader.getFileNameLength()).isEqualTo(FILE_NAME_IN_ZIP.length());
  verifyGeneralPurposeBytes(fileHeader.getGeneralPurposeFlag(), zipParameters);
  assertThat(fileHeader.getDiskNumberStart()).isEqualTo(isSplitZip ? diskNumberStart : 0);
  verifyLastModifiedFileTime(fileHeader, zipParameters);
  assertThat(fileHeader.getExternalFileAttributes()).isEqualTo(new byte[4]);
  assertThat(fileHeader.isDirectory()).isEqualTo(false);

  if (zipParameters.isWriteExtendedLocalFileHeader()) {
    assertThat(fileHeader.getUncompressedSize()).isEqualTo(0);
  } else {
    assertThat(fileHeader.getUncompressedSize()).isEqualTo(zipParameters.getEntrySize());
  }
  verifyCrc(fileHeader);
  assertThat(fileHeader.isDataDescriptorExists()).isEqualTo(zipParameters.isWriteExtendedLocalFileHeader());
  assertThat(fileHeader.getAesExtraDataRecord() != null).isEqualTo(aesExtraDataRecordPresent);
  assertThat(fileHeader.getExtraFieldLength()).isEqualTo(aesExtraDataRecordPresent ? InternalZipConstants.AES_EXTRA_DATA_RECORD_SIZE : 0);
}
 
Example 5
Source File: FileHeaderFactory.java    From zip4j with Apache License 2.0 4 votes vote down vote up
public FileHeader generateFileHeader(ZipParameters zipParameters, boolean isSplitZip, int currentDiskNumberStart,
                                     Charset charset, RawIO rawIO)
    throws ZipException {

  FileHeader fileHeader = new FileHeader();
  fileHeader.setSignature(HeaderSignature.CENTRAL_DIRECTORY);
  fileHeader.setVersionMadeBy(determineVersionMadeBy(zipParameters, rawIO));
  fileHeader.setVersionNeededToExtract(determineVersionNeededToExtract(zipParameters).getCode());

  if (zipParameters.isEncryptFiles() && zipParameters.getEncryptionMethod() == EncryptionMethod.AES) {
    fileHeader.setCompressionMethod(CompressionMethod.AES_INTERNAL_ONLY);
    fileHeader.setAesExtraDataRecord(generateAESExtraDataRecord(zipParameters));
    fileHeader.setExtraFieldLength(fileHeader.getExtraFieldLength() + InternalZipConstants.AES_EXTRA_DATA_RECORD_SIZE);
  } else {
    fileHeader.setCompressionMethod(zipParameters.getCompressionMethod());
  }

  if (zipParameters.isEncryptFiles()) {
    if (zipParameters.getEncryptionMethod() == null || zipParameters.getEncryptionMethod() == EncryptionMethod.NONE) {
      throw new ZipException("Encryption method has to be set when encryptFiles flag is set in zip parameters");
    }

    fileHeader.setEncrypted(true);
    fileHeader.setEncryptionMethod(zipParameters.getEncryptionMethod());
  }

  String fileName = validateAndGetFileName(zipParameters.getFileNameInZip());
  fileHeader.setFileName(fileName);
  fileHeader.setFileNameLength(determineFileNameLength(fileName, charset));
  fileHeader.setDiskNumberStart(isSplitZip ? currentDiskNumberStart : 0);

  if (zipParameters.getLastModifiedFileTime() > 0) {
    fileHeader.setLastModifiedTime(Zip4jUtil.javaToDosTime(zipParameters.getLastModifiedFileTime()));
  } else {
    fileHeader.setLastModifiedTime(Zip4jUtil.javaToDosTime(System.currentTimeMillis()));
  }

  //For files added by this library, this attribute will be set after closeEntry is done
  fileHeader.setExternalFileAttributes(new byte[4]);
  fileHeader.setDirectory(isZipEntryDirectory(fileName));

  if (zipParameters.isWriteExtendedLocalFileHeader() && zipParameters.getEntrySize() == -1) {
    fileHeader.setUncompressedSize(0);
  } else {
    fileHeader.setUncompressedSize(zipParameters.getEntrySize());
  }

  if (zipParameters.isEncryptFiles() && zipParameters.getEncryptionMethod() == EncryptionMethod.ZIP_STANDARD) {
    fileHeader.setCrc(zipParameters.getEntryCRC());
  }

  fileHeader.setGeneralPurposeFlag(determineGeneralPurposeBitFlag(fileHeader.isEncrypted(), zipParameters, charset));
  fileHeader.setDataDescriptorExists(zipParameters.isWriteExtendedLocalFileHeader());
  fileHeader.setFileComment(zipParameters.getFileComment());
  return fileHeader;
}