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

The following examples show how to use net.lingala.zip4j.model.LocalFileHeader#setUncompressedSize() . 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 6 votes vote down vote up
public LocalFileHeader generateLocalFileHeader(FileHeader fileHeader) {
  LocalFileHeader localFileHeader = new LocalFileHeader();
  localFileHeader.setSignature(HeaderSignature.LOCAL_FILE_HEADER);
  localFileHeader.setVersionNeededToExtract(fileHeader.getVersionNeededToExtract());
  localFileHeader.setCompressionMethod(fileHeader.getCompressionMethod());
  localFileHeader.setLastModifiedTime(fileHeader.getLastModifiedTime());
  localFileHeader.setUncompressedSize(fileHeader.getUncompressedSize());
  localFileHeader.setFileNameLength(fileHeader.getFileNameLength());
  localFileHeader.setFileName(fileHeader.getFileName());
  localFileHeader.setEncrypted(fileHeader.isEncrypted());
  localFileHeader.setEncryptionMethod(fileHeader.getEncryptionMethod());
  localFileHeader.setAesExtraDataRecord(fileHeader.getAesExtraDataRecord());
  localFileHeader.setCrc(fileHeader.getCrc());
  localFileHeader.setCompressedSize(fileHeader.getCompressedSize());
  localFileHeader.setGeneralPurposeFlag(fileHeader.getGeneralPurposeFlag().clone());
  localFileHeader.setDataDescriptorExists(fileHeader.isDataDescriptorExists());
  localFileHeader.setExtraFieldLength(fileHeader.getExtraFieldLength());
  return localFileHeader;
}
 
Example 2
Source File: HeaderReader.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void readZip64ExtendedInfo(LocalFileHeader localFileHeader, RawIO rawIO) throws ZipException {
  if (localFileHeader == null) {
    throw new ZipException("file header is null in reading Zip64 Extended Info");
  }

  if (localFileHeader.getExtraDataRecords() == null || localFileHeader.getExtraDataRecords().size() <= 0) {
    return;
  }

  Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(localFileHeader.getExtraDataRecords(), rawIO,
      localFileHeader.getUncompressedSize(), localFileHeader.getCompressedSize(), 0, 0);

  if (zip64ExtendedInfo == null) {
    return;
  }

  localFileHeader.setZip64ExtendedInfo(zip64ExtendedInfo);

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

  if (zip64ExtendedInfo.getCompressedSize() != -1) {
    localFileHeader.setCompressedSize(zip64ExtendedInfo.getCompressedSize());
  }
}
 
Example 3
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void createAndUpdateLocalFileHeader(File headersFile, long compressedSize, long uncompressedSize, long crc)
    throws IOException {
  ZipModel zipModel = createZipModel(3);
  LocalFileHeader localFileHeaderToWrite = createLocalFileHeader("LFH", compressedSize, uncompressedSize, false);
  localFileHeaderToWrite.setCompressedSize(compressedSize);
  localFileHeaderToWrite.setUncompressedSize(uncompressedSize);
  localFileHeaderToWrite.setCrc(10);

  try (OutputStream outputStream = new FileOutputStream(headersFile)) {
    headerWriter.writeLocalFileHeader(zipModel, localFileHeaderToWrite, outputStream, InternalZipConstants.CHARSET_UTF_8);
  }

  try (SplitOutputStream splitOutputStream = new SplitOutputStream(headersFile)) {
    FileHeader fileHeader = createFileHeaders(1, compressedSize + 100, uncompressedSize + 100).get(0);
    fileHeader.setFileName(localFileHeaderToWrite.getFileName());
    fileHeader.setFileNameLength(fileHeader.getFileName().getBytes(InternalZipConstants.CHARSET_UTF_8).length);
    fileHeader.setCrc(crc);
    headerWriter.updateLocalFileHeader(fileHeader, zipModel, splitOutputStream);
  }
}
 
Example 4
Source File: HeaderReader.java    From AndroidZip with Apache License 2.0 6 votes vote down vote up
/**
 * Reads Zip64 Extended Info and saves it in the {@link LocalFileHeader}
 * @param localFileHeader
 * @throws ZipException
 */
private void readAndSaveZip64ExtendedInfo(LocalFileHeader localFileHeader) throws ZipException {
	if (localFileHeader == null) {
		throw new ZipException("file header is null in reading Zip64 Extended Info");
	}
	
	if (localFileHeader.getExtraDataRecords() == null || localFileHeader.getExtraDataRecords().size() <= 0) {
		return;
	}
	
	Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(
			localFileHeader.getExtraDataRecords(), 
			localFileHeader.getUncompressedSize(), 
			localFileHeader.getCompressedSize(), 
			-1, -1);
	
	if (zip64ExtendedInfo != null) {
		localFileHeader.setZip64ExtendedInfo(zip64ExtendedInfo);
		
		if (zip64ExtendedInfo.getUnCompressedSize() != -1)
			localFileHeader.setUncompressedSize(zip64ExtendedInfo.getUnCompressedSize());
		
		if (zip64ExtendedInfo.getCompressedSize() != -1)
			localFileHeader.setCompressedSize(zip64ExtendedInfo.getCompressedSize());
	}
}
 
Example 5
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private LocalFileHeader createLocalFileHeader(String fileNameSuffix, long compressedSize, long uncompressedSize,
                                              boolean useUtf8) {
  LocalFileHeader localFileHeader = new LocalFileHeader();
  localFileHeader.setVersionNeededToExtract(VERSION_NEEDED_TO_EXTRACT);
  localFileHeader.setFileName(FILE_NAME_PREFIX + fileNameSuffix);
  localFileHeader.setCompressedSize(compressedSize);
  localFileHeader.setUncompressedSize(uncompressedSize);
  localFileHeader.setGeneralPurposeFlag(generateGeneralPurposeBytes(useUtf8));
  localFileHeader.setCompressionMethod(CompressionMethod.DEFLATE);
  localFileHeader.setLastModifiedTime(LAST_MODIFIED_FILE_TIME);
  return localFileHeader;
}
 
Example 6
Source File: HeaderReaderIT.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private LocalFileHeader generateLocalFileHeader(long entrySize, EncryptionMethod encryptionMethod)
    throws ZipException {
  List<FileHeader> fileHeaders = generateFileHeaders(generateZipParameters(encryptionMethod), 1);
  LocalFileHeader localFileHeader = fileHeaderFactory.generateLocalFileHeader(fileHeaders.get(0));
  localFileHeader.setCompressedSize(entrySize);
  localFileHeader.setUncompressedSize(entrySize);
  return localFileHeader;
}
 
Example 7
Source File: HeaderReader.java    From zip4j with Apache License 2.0 4 votes vote down vote up
public LocalFileHeader readLocalFileHeader(InputStream inputStream, Charset charset) throws IOException {
    LocalFileHeader localFileHeader = new LocalFileHeader();
    byte[] intBuff = new byte[4];

    //signature
    int sig = rawIO.readIntLittleEndian(inputStream);
    if (sig != HeaderSignature.LOCAL_FILE_HEADER.getValue()) {
      return null;
    }
    localFileHeader.setSignature(HeaderSignature.LOCAL_FILE_HEADER);
    localFileHeader.setVersionNeededToExtract(rawIO.readShortLittleEndian(inputStream));

    byte[] generalPurposeFlags = new byte[2];
    if (readFully(inputStream, generalPurposeFlags) != 2) {
      throw new ZipException("Could not read enough bytes for generalPurposeFlags");
    }
    localFileHeader.setEncrypted(isBitSet(generalPurposeFlags[0], 0));
    localFileHeader.setDataDescriptorExists(isBitSet(generalPurposeFlags[0], 3));
    localFileHeader.setFileNameUTF8Encoded(isBitSet(generalPurposeFlags[1], 3));
    localFileHeader.setGeneralPurposeFlag(generalPurposeFlags.clone());

    localFileHeader.setCompressionMethod(CompressionMethod.getCompressionMethodFromCode(
        rawIO.readShortLittleEndian(inputStream)));
    localFileHeader.setLastModifiedTime(rawIO.readIntLittleEndian(inputStream));

    readFully(inputStream, intBuff);
    localFileHeader.setCrc(rawIO.readLongLittleEndian(intBuff, 0));
    localFileHeader.setCrcRawData(intBuff.clone());

    localFileHeader.setCompressedSize(rawIO.readLongLittleEndian(inputStream, 4));
    localFileHeader.setUncompressedSize(rawIO.readLongLittleEndian(inputStream, 4));

    int fileNameLength = rawIO.readShortLittleEndian(inputStream);
    localFileHeader.setFileNameLength(fileNameLength);

    localFileHeader.setExtraFieldLength(rawIO.readShortLittleEndian(inputStream));

    if (fileNameLength > 0) {
      byte[] fileNameBuf = new byte[fileNameLength];
      readFully(inputStream, fileNameBuf);
      // Modified after user reported an issue http://www.lingala.net/zip4j/forum/index.php?topic=2.0
//				String fileName = new String(fileNameBuf, "Cp850");
//				String fileName = Zip4jUtil.getCp850EncodedString(fileNameBuf);
      String fileName = decodeStringWithCharset(fileNameBuf, localFileHeader.isFileNameUTF8Encoded(), charset);

      if (fileName == null) {
        throw new ZipException("file name is null, cannot assign file name to local file header");
      }

      if (fileName.contains(":" + System.getProperty("file.separator"))) {
        fileName = fileName.substring(fileName.indexOf(":" + System.getProperty("file.separator")) + 2);
      }

      localFileHeader.setFileName(fileName);
      localFileHeader.setDirectory(fileName.endsWith("/") || fileName.endsWith("\\"));
    } else {
      localFileHeader.setFileName(null);
    }

    readExtraDataRecords(inputStream, localFileHeader);
    readZip64ExtendedInfo(localFileHeader, rawIO);
    readAesExtraDataRecord(localFileHeader, rawIO);

    if (localFileHeader.isEncrypted()) {

      if (localFileHeader.getEncryptionMethod() == EncryptionMethod.AES) {
        //Do nothing
      } else {
        if (BigInteger.valueOf(localFileHeader.getGeneralPurposeFlag()[0]).testBit(6)) {
          localFileHeader.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD_VARIANT_STRONG);
        } else {
          localFileHeader.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        }
      }

    }

    return localFileHeader;
  }