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

The following examples show how to use net.lingala.zip4j.model.LocalFileHeader#setGeneralPurposeFlag() . 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 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 3
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;
  }