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

The following examples show how to use net.lingala.zip4j.model.LocalFileHeader#setCrc() . 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: 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 3
Source File: HeaderWriterIT.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void testWriteExtendedLocalFileHeader(long compressedSize, long uncompressedSize, long crc,
                                              boolean isZip64Format) throws IOException {
  LocalFileHeader localFileHeader = createLocalFileHeader("SOME_NAME", compressedSize, uncompressedSize, true);
  localFileHeader.setCrc(crc);
  localFileHeader.setWriteCompressedSizeInZip64ExtraRecord(isZip64Format);
  File headersFile = temporaryFolder.newFile();

  try(OutputStream outputStream = new FileOutputStream(headersFile)) {
    headerWriter.writeExtendedLocalHeader(localFileHeader, outputStream);
  }

  try(InputStream inputStream = new FileInputStream(headersFile)) {
    DataDescriptor dataDescriptor = headerReader.readDataDescriptor(inputStream, isZip64Format);
    verifyDataDescriptor(dataDescriptor, compressedSize, uncompressedSize, crc);
  }
}
 
Example 4
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;
  }