Java Code Examples for net.lingala.zip4j.model.FileHeader#getExtraDataRecords()

The following examples show how to use net.lingala.zip4j.model.FileHeader#getExtraDataRecords() . 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: HeaderWriter.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private int calculateExtraDataRecordsSize(FileHeader fileHeader, boolean writeZip64ExtendedInfo) throws IOException {
  int extraFieldLength = 0;

  if (writeZip64ExtendedInfo) {
    extraFieldLength += ZIP64_EXTRA_DATA_RECORD_SIZE_FH + 4; // 4 for signature + size of record
  }

  if (fileHeader.getAesExtraDataRecord() != null) {
    extraFieldLength += AES_EXTRA_DATA_RECORD_SIZE;
  }

  if (fileHeader.getExtraDataRecords() != null) {
    for (ExtraDataRecord extraDataRecord : fileHeader.getExtraDataRecords()) {
      if (extraDataRecord.getHeader() == HeaderSignature.AES_EXTRA_DATA_RECORD.getValue()
          || extraDataRecord.getHeader() == HeaderSignature.ZIP64_EXTRA_FIELD_SIGNATURE.getValue()) {
        continue;
      }

      extraFieldLength += 4 + extraDataRecord.getSizeOfData(); // 4  = 2 for header + 2 for size of data
    }
  }

  return extraFieldLength;
}
 
Example 2
Source File: HeaderWriter.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void writeRemainingExtraDataRecordsIfPresent(FileHeader fileHeader, OutputStream outputStream) throws IOException {
  if (fileHeader.getExtraDataRecords() == null || fileHeader.getExtraDataRecords().size() == 0) {
    return;
  }

  for (ExtraDataRecord extraDataRecord : fileHeader.getExtraDataRecords()) {
    if (extraDataRecord.getHeader() == HeaderSignature.AES_EXTRA_DATA_RECORD.getValue()
          || extraDataRecord.getHeader() == HeaderSignature.ZIP64_EXTRA_FIELD_SIGNATURE.getValue()) {
      continue;
    }

    rawIO.writeShortLittleEndian(outputStream, (int) extraDataRecord.getHeader());
    rawIO.writeShortLittleEndian(outputStream, extraDataRecord.getSizeOfData());

    if (extraDataRecord.getSizeOfData() > 0 && extraDataRecord.getData() != null) {
      outputStream.write(extraDataRecord.getData());
    }
  }
}
 
Example 3
Source File: HeaderReader.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
/**
 * Reads AES Extra Data Record and saves it in the {@link FileHeader}
 * @param fileHeader
 * @throws ZipException
 */
private void readAndSaveAESExtraDataRecord(FileHeader fileHeader) throws ZipException {
	if (fileHeader == null) {
		throw new ZipException("file header is null in reading Zip64 Extended Info");
	}
	
	if (fileHeader.getExtraDataRecords() == null || fileHeader.getExtraDataRecords().size() <= 0) {
		return;
	}
	
	AESExtraDataRecord aesExtraDataRecord = readAESExtraDataRecord(fileHeader.getExtraDataRecords());
	if (aesExtraDataRecord != null) {
		fileHeader.setAesExtraDataRecord(aesExtraDataRecord);
		fileHeader.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
	}
}
 
Example 4
Source File: HeaderReader.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void readZip64ExtendedInfo(FileHeader fileHeader, RawIO rawIO) throws ZipException {
  if (fileHeader.getExtraDataRecords() == null || fileHeader.getExtraDataRecords().size() <= 0) {
    return;
  }

  Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(fileHeader.getExtraDataRecords(), rawIO,
      fileHeader.getUncompressedSize(), fileHeader.getCompressedSize(), fileHeader.getOffsetLocalHeader(),
      fileHeader.getDiskNumberStart());

  if (zip64ExtendedInfo == null) {
    return;
  }

  fileHeader.setZip64ExtendedInfo(zip64ExtendedInfo);

  if (zip64ExtendedInfo.getUncompressedSize() != -1) {
    fileHeader.setUncompressedSize(zip64ExtendedInfo.getUncompressedSize());
  }

  if (zip64ExtendedInfo.getCompressedSize() != -1) {
    fileHeader.setCompressedSize(zip64ExtendedInfo.getCompressedSize());
  }

  if (zip64ExtendedInfo.getOffsetLocalHeader() != -1) {
    fileHeader.setOffsetLocalHeader(zip64ExtendedInfo.getOffsetLocalHeader());
  }

  if (zip64ExtendedInfo.getDiskNumberStart() != -1) {
    fileHeader.setDiskNumberStart(zip64ExtendedInfo.getDiskNumberStart());
  }
}
 
Example 5
Source File: HeaderReader.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void readAesExtraDataRecord(FileHeader fileHeader, RawIO rawIO) throws ZipException {
  if (fileHeader.getExtraDataRecords() == null || fileHeader.getExtraDataRecords().size() <= 0) {
    return;
  }

  AESExtraDataRecord aesExtraDataRecord = readAesExtraDataRecord(fileHeader.getExtraDataRecords(), rawIO);
  if (aesExtraDataRecord != null) {
    fileHeader.setAesExtraDataRecord(aesExtraDataRecord);
    fileHeader.setEncryptionMethod(EncryptionMethod.AES);
  }
}
 
Example 6
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void addExtraDataRecordToFirstFileHeader(ZipModel zipModel, byte[] data) {
  ExtraDataRecord extraDataRecord = new ExtraDataRecord();
  extraDataRecord.setHeader(12345);
  extraDataRecord.setSizeOfData(data == null ? 0 : data.length);
  extraDataRecord.setData(data);

  FileHeader firstFileHeader = zipModel.getCentralDirectory().getFileHeaders().get(0);

  if (firstFileHeader.getExtraDataRecords() == null) {
    firstFileHeader.setExtraDataRecords(new ArrayList<>());
  }

  firstFileHeader.getExtraDataRecords().add(extraDataRecord);
}
 
Example 7
Source File: HeaderReader.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
/**
 * Reads Zip64 Extended info and saves it in the {@link FileHeader}
 * @param fileHeader
 * @throws ZipException
 */
private void readAndSaveZip64ExtendedInfo(FileHeader fileHeader) throws ZipException {
	if (fileHeader == null) {
		throw new ZipException("file header is null in reading Zip64 Extended Info");
	}
	
	if (fileHeader.getExtraDataRecords() == null || fileHeader.getExtraDataRecords().size() <= 0) {
		return;
	}
	
	Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(
			fileHeader.getExtraDataRecords(), 
			fileHeader.getUncompressedSize(), 
			fileHeader.getCompressedSize(), 
			fileHeader.getOffsetLocalHeader(), 
			fileHeader.getDiskNumberStart());
	
	if (zip64ExtendedInfo != null) {
		fileHeader.setZip64ExtendedInfo(zip64ExtendedInfo);
		if (zip64ExtendedInfo.getUnCompressedSize() != -1)
			fileHeader.setUncompressedSize(zip64ExtendedInfo.getUnCompressedSize());
		
		if (zip64ExtendedInfo.getCompressedSize() != -1)
			fileHeader.setCompressedSize(zip64ExtendedInfo.getCompressedSize());
		
		if (zip64ExtendedInfo.getOffsetLocalHeader() != -1)
			fileHeader.setOffsetLocalHeader(zip64ExtendedInfo.getOffsetLocalHeader());
		
		if (zip64ExtendedInfo.getDiskNumberStart() != -1)
			fileHeader.setDiskNumberStart(zip64ExtendedInfo.getDiskNumberStart());
	}
}