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

The following examples show how to use net.lingala.zip4j.model.FileHeader#getFileName() . 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: HeaderVerifier.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private static InputStream positionRandomAccessFileToLocalFileHeaderStart(File generatedZipFile, String fileNameInZip)
    throws IOException{

  ZipFile zipFile = new ZipFile(generatedZipFile);
  FileHeader fileHeader = zipFile.getFileHeader(fileNameInZip);

  if (fileHeader == null) {
    throw new RuntimeException("Cannot find an entry with name: " + fileNameInZip + " in zip file: "
        + generatedZipFile);
  }

  InputStream inputStream = new FileInputStream(generatedZipFile);
  if (inputStream.skip(fileHeader.getOffsetLocalHeader()) != fileHeader.getOffsetLocalHeader()) {
    throw new IOException("Cannot skip " + fileHeader.getOffsetLocalHeader() + " bytes for entry "
        + fileHeader.getFileName());
  }
  return inputStream;
}
 
Example 2
Source File: BuildTool.java    From thorntail with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void expandArtifact(File artifactFile) throws IOException {
    try {
        ZipFile zipFile = new ZipFile(artifactFile);
        for (FileHeader each : (List<FileHeader>) zipFile.getFileHeaders()) {
            String fileName = each.getFileName();
            if (fileName.startsWith("META-INF") && !fileName.startsWith("META-INF/versions")) {
                continue;
            }
            if (each.isDirectory()) {
                continue;
            }
            this.archive.add(new ZipFileHeaderAsset(zipFile, each), fileName);
        }
    } catch (ZipException e) {
        throw new IOException(e);
    }
}
 
Example 3
Source File: RenameFilesTask.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void updateHeadersInZipModel(FileHeader fileHeader, String newFileName, byte[] newFileNameBytes,
                                     int headersOffset) throws ZipException {

  FileHeader fileHeaderToBeChanged = HeaderUtil.getFileHeader(zipModel, fileHeader.getFileName());

  if (fileHeaderToBeChanged == null) {
    // If this is the case, then the file name in the header that was passed to this method was already changed.
    // In theory, should never be here.
    throw new ZipException("could not find any header with name: " + fileHeader.getFileName());
  }

  fileHeaderToBeChanged.setFileName(newFileName);
  fileHeaderToBeChanged.setFileNameLength(newFileNameBytes.length);

  updateOffsetsForAllSubsequentFileHeaders(zipModel, fileHeaderToBeChanged, headersOffset);

  zipModel.getEndOfCentralDirectoryRecord().setOffsetOfStartOfCentralDirectory(
      zipModel.getEndOfCentralDirectoryRecord().getOffsetOfStartOfCentralDirectory() + headersOffset);

  if (zipModel.isZip64Format()) {
    zipModel.getZip64EndOfCentralDirectoryRecord().setOffsetStartCentralDirectoryWRTStartDiskNumber(
        zipModel.getZip64EndOfCentralDirectoryRecord().getOffsetStartCentralDirectoryWRTStartDiskNumber() + headersOffset
    );

    zipModel.getZip64EndOfCentralDirectoryLocator().setOffsetZip64EndOfCentralDirectoryRecord(
        zipModel.getZip64EndOfCentralDirectoryLocator().getOffsetZip64EndOfCentralDirectoryRecord() + headersOffset
    );
  }
}
 
Example 4
Source File: AbstractExtractFileTask.java    From zip4j with Apache License 2.0 5 votes vote down vote up
protected void extractFile(ZipInputStream zipInputStream, FileHeader fileHeader, String outputPath,
                           String newFileName, ProgressMonitor progressMonitor) throws IOException {

  if (!outputPath.endsWith(FILE_SEPARATOR)) {
    outputPath += FILE_SEPARATOR;
  }

  File outputFile = determineOutputFile(fileHeader, outputPath, newFileName);
  progressMonitor.setFileName(outputFile.getAbsolutePath());

  // make sure no file is extracted outside of the target directory (a.k.a zip slip)
  String outputCanonicalPath = (new File(outputPath).getCanonicalPath()) + File.separator;
  if (!outputFile.getCanonicalPath().startsWith(outputCanonicalPath)) {
    throw new ZipException("illegal file name that breaks out of the target directory: "
        + fileHeader.getFileName());
  }

  verifyNextEntry(zipInputStream, fileHeader);

  if (fileHeader.isDirectory()) {
    if (!outputFile.exists()) {
      if (!outputFile.mkdirs()) {
        throw new ZipException("Could not create directory: " + outputFile);
      }
    }
  } else if (isSymbolicLink(fileHeader)) {
    createSymLink(zipInputStream, fileHeader, outputFile, progressMonitor);
  } else {
    checkOutputDirectoryStructure(outputFile);
    unzipFile(zipInputStream, fileHeader, outputFile, progressMonitor);
  }
}
 
Example 5
Source File: AbstractExtractFileTask.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private void verifyNextEntry(ZipInputStream zipInputStream, FileHeader fileHeader) throws IOException {
  LocalFileHeader localFileHeader = zipInputStream.getNextEntry(fileHeader);

  if (localFileHeader == null) {
    throw new ZipException("Could not read corresponding local file header for file header: "
        + fileHeader.getFileName());
  }

  if (!fileHeader.getFileName().equals(localFileHeader.getFileName())) {
    throw new ZipException("File header and local file header mismatch");
  }
}
 
Example 6
Source File: HeaderUtil.java    From zip4j with Apache License 2.0 5 votes vote down vote up
public static int getIndexOfFileHeader(ZipModel zipModel, FileHeader fileHeader) throws ZipException {

    if (zipModel == null || fileHeader == null) {
      throw new ZipException("input parameters is null, cannot determine index of file header");
    }

    if (zipModel.getCentralDirectory() == null
        || zipModel.getCentralDirectory().getFileHeaders() == null
        || zipModel.getCentralDirectory().getFileHeaders().size() <= 0) {
      return -1;
    }

    String fileName = fileHeader.getFileName();

    if (!isStringNotNullAndNotEmpty(fileName)) {
      throw new ZipException("file name in file header is empty or null, cannot determine index of file header");
    }

    List<FileHeader> fileHeadersFromCentralDir = zipModel.getCentralDirectory().getFileHeaders();
    for (int i = 0; i < fileHeadersFromCentralDir.size(); i++) {
      FileHeader fileHeaderFromCentralDir = fileHeadersFromCentralDir.get(i);
      String fileNameForHdr = fileHeaderFromCentralDir.getFileName();
      if (!isStringNotNullAndNotEmpty(fileNameForHdr)) {
        continue;
      }

      if (fileName.equalsIgnoreCase(fileNameForHdr)) {
        return i;
      }
    }
    return -1;
  }
 
Example 7
Source File: HeaderUtil.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private static FileHeader getFileHeaderWithExactMatch(ZipModel zipModel, String fileName) throws ZipException {
  if (zipModel == null) {
    throw new ZipException("zip model is null, cannot determine file header with exact match for fileName: "
        + fileName);
  }

  if (!isStringNotNullAndNotEmpty(fileName)) {
    throw new ZipException("file name is null, cannot determine file header with exact match for fileName: "
        + fileName);
  }

  if (zipModel.getCentralDirectory() == null) {
    throw new ZipException("central directory is null, cannot determine file header with exact match for fileName: "
        + fileName);
  }

  if (zipModel.getCentralDirectory().getFileHeaders() == null) {
    throw new ZipException("file Headers are null, cannot determine file header with exact match for fileName: "
        + fileName);
  }

  if (zipModel.getCentralDirectory().getFileHeaders().size() == 0) {
    return null;
  }

  for (FileHeader fileHeader : zipModel.getCentralDirectory().getFileHeaders()) {
    String fileNameForHdr = fileHeader.getFileName();
    if (!isStringNotNullAndNotEmpty(fileNameForHdr)) {
      continue;
    }

    if (fileName.equalsIgnoreCase(fileNameForHdr)) {
      return fileHeader;
    }
  }

  return null;
}
 
Example 8
Source File: Unzip.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
private void checkOutputDirectoryStructure(FileHeader fileHeader, String outPath, String newFileName) throws ZipException {
	if (fileHeader == null || !Zip4jUtil.isStringNotNullAndNotEmpty(outPath)) {
		throw new ZipException("Cannot check output directory structure...one of the parameters was null");
	}
	
	String fileName = fileHeader.getFileName();
	
	if (Zip4jUtil.isStringNotNullAndNotEmpty(newFileName)) {
		fileName = newFileName;
	}
	
	if (!Zip4jUtil.isStringNotNullAndNotEmpty(fileName)) {
		// Do nothing
		return;
	}
	
	String compOutPath = outPath + fileName;
	try {
		File file = new File(compOutPath);
		String parentDir = file.getParent();
		File parentDirFile = new File(parentDir);
		if (!parentDirFile.exists()) {
			parentDirFile.mkdirs();
		}
	} catch (Exception e) {
		throw new ZipException(e);
	}
}
 
Example 9
Source File: Zip4jUtil.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
public static FileHeader getFileHeaderWithExactMatch(ZipModel zipModel, String fileName) throws ZipException {
	if (zipModel == null) {
		throw new ZipException("zip model is null, cannot determine file header with exact match for fileName: " + fileName);
	}
	
	if (!isStringNotNullAndNotEmpty(fileName)) {
		throw new ZipException("file name is null, cannot determine file header with exact match for fileName: " + fileName);
	}
	
	if (zipModel.getCentralDirectory() == null) {
		throw new ZipException("central directory is null, cannot determine file header with exact match for fileName: " + fileName);
	}
	
	if (zipModel.getCentralDirectory().getFileHeaders() == null) {
		throw new ZipException("file Headers are null, cannot determine file header with exact match for fileName: " + fileName);
	}
	
	if (zipModel.getCentralDirectory().getFileHeaders().size() <= 0) {
		return null;
	}
	ArrayList fileHeaders = zipModel.getCentralDirectory().getFileHeaders();
	for (int i = 0; i < fileHeaders.size(); i++) {
		FileHeader fileHeader = (FileHeader)fileHeaders.get(i);
		String fileNameForHdr = fileHeader.getFileName();
		if (!isStringNotNullAndNotEmpty(fileNameForHdr)) {
			continue;
		}
		
		if (fileName.equalsIgnoreCase(fileNameForHdr)) {
			return fileHeader;
		}
	}
	
	return null;
}
 
Example 10
Source File: Zip4jUtil.java    From AndroidZip with Apache License 2.0 5 votes vote down vote up
public static int getIndexOfFileHeader(ZipModel zipModel, 
		FileHeader fileHeader) throws ZipException {
	
	if (zipModel == null || fileHeader == null) {
		throw new ZipException("input parameters is null, cannot determine index of file header");
	}
	
	if (zipModel.getCentralDirectory() == null) {
		throw new ZipException("central directory is null, ccannot determine index of file header");
	}
	
	if (zipModel.getCentralDirectory().getFileHeaders() == null) {
		throw new ZipException("file Headers are null, cannot determine index of file header");
	}
	
	if (zipModel.getCentralDirectory().getFileHeaders().size() <= 0) {
		return -1;
	}
	String fileName = fileHeader.getFileName();
	
	if (!isStringNotNullAndNotEmpty(fileName)) {
		throw new ZipException("file name in file header is empty or null, cannot determine index of file header");
	}
	
	ArrayList fileHeaders = zipModel.getCentralDirectory().getFileHeaders();
	for (int i = 0; i < fileHeaders.size(); i++) {
		FileHeader fileHeaderTmp = (FileHeader)fileHeaders.get(i);
		String fileNameForHdr = fileHeaderTmp.getFileName();
		if (!isStringNotNullAndNotEmpty(fileNameForHdr)) {
			continue;
		}
		
		if (fileName.equalsIgnoreCase(fileNameForHdr)) {
			return i;
		}
	}
	return -1;
}