Java Code Examples for net.lingala.zip4j.progress.ProgressMonitor#updateWorkCompleted()

The following examples show how to use net.lingala.zip4j.progress.ProgressMonitor#updateWorkCompleted() . 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: ExtractAllFilesTask.java    From zip4j with Apache License 2.0 6 votes vote down vote up
@Override
protected void executeTask(ExtractAllFilesTaskParameters taskParameters, ProgressMonitor progressMonitor)
    throws IOException {
  try (ZipInputStream zipInputStream = prepareZipInputStream(taskParameters.charset)) {
    for (FileHeader fileHeader : getZipModel().getCentralDirectory().getFileHeaders()) {
      if (fileHeader.getFileName().startsWith("__MACOSX")) {
        progressMonitor.updateWorkCompleted(fileHeader.getUncompressedSize());
        continue;
      }

      splitInputStream.prepareExtractionForFileHeader(fileHeader);

      extractFile(zipInputStream, fileHeader, taskParameters.outputPath, null, progressMonitor);
      verifyIfTaskIsCancelled();
    }
  } finally {
    if (splitInputStream != null) {
      splitInputStream.close();
    }
  }
}
 
Example 2
Source File: AbstractAddFileToZipTask.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void addFileToZip(File fileToAdd, ZipOutputStream zipOutputStream, ZipParameters zipParameters,
                          SplitOutputStream splitOutputStream, ProgressMonitor progressMonitor) throws IOException {

  zipOutputStream.putNextEntry(zipParameters);

  if (!fileToAdd.isDirectory()) {
    try (InputStream inputStream = new FileInputStream(fileToAdd)) {
      while ((readLen = inputStream.read(readBuff)) != -1) {
        zipOutputStream.write(readBuff, 0, readLen);
        progressMonitor.updateWorkCompleted(readLen);
        verifyIfTaskIsCancelled();
      }
    }
  }

  closeEntry(zipOutputStream, splitOutputStream, fileToAdd, false);
}
 
Example 3
Source File: AbstractExtractFileTask.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private void unzipFile(ZipInputStream inputStream, FileHeader fileHeader, File outputFile,
                       ProgressMonitor progressMonitor) throws IOException {
  int readLength;
  try (OutputStream outputStream = new FileOutputStream(outputFile)) {
    while ((readLength = inputStream.read(buff)) != -1) {
      outputStream.write(buff, 0, readLength);
      progressMonitor.updateWorkCompleted(readLength);
      verifyIfTaskIsCancelled();
    }
  } catch (Exception e) {
    if (outputFile.exists()) {
      outputFile.delete();
    }
    throw  e;
  }

  UnzipUtil.applyFileAttributes(fileHeader, outputFile);
}
 
Example 4
Source File: AbstractExtractFileTask.java    From zip4j with Apache License 2.0 5 votes vote down vote up
private byte[] readCompleteEntry(ZipInputStream zipInputStream, FileHeader fileHeader,
                                 ProgressMonitor progressMonitor) throws IOException {
  byte[] b = new byte[(int) fileHeader.getUncompressedSize()];
  int readLength = zipInputStream.read(b);

  if (readLength != b.length) {
    throw new ZipException("Could not read complete entry");
  }

  progressMonitor.updateWorkCompleted(b.length);
  return b;
}
 
Example 5
Source File: CrcUtil.java    From zip4j with Apache License 2.0 5 votes vote down vote up
public static long computeFileCrc(File inputFile, ProgressMonitor progressMonitor) throws IOException {

    if (inputFile == null || !inputFile.exists() || !inputFile.canRead()) {
      throw new ZipException("input file is null or does not exist or cannot read. " +
          "Cannot calculate CRC for the file");
    }

    byte[] buff = new byte[BUF_SIZE];
    CRC32 crc32 = new CRC32();

    try(InputStream inputStream = new FileInputStream(inputFile)) {
      int readLen;
      while ((readLen = inputStream.read(buff)) != -1) {
        crc32.update(buff, 0, readLen);

        if (progressMonitor != null) {
          progressMonitor.updateWorkCompleted(readLen);
          if (progressMonitor.isCancelAllTasks()) {
            progressMonitor.setResult(ProgressMonitor.Result.CANCELLED);
            progressMonitor.setState(ProgressMonitor.State.READY);
            return 0;
          }
        }
      }
      return crc32.getValue();
    }
  }
 
Example 6
Source File: FileUtils.java    From zip4j with Apache License 2.0 4 votes vote down vote up
public static void copyFile(RandomAccessFile randomAccessFile, OutputStream outputStream, long start, long end,
                            ProgressMonitor progressMonitor) throws ZipException {

  if (start < 0 || end < 0 || start > end) {
    throw new ZipException("invalid offsets");
  }

  if (start == end) {
    return;
  }

  try {
    randomAccessFile.seek(start);

    int readLen;
    byte[] buff;
    long bytesRead = 0;
    long bytesToRead = end - start;

    if ((end - start) < BUFF_SIZE) {
      buff = new byte[(int) bytesToRead];
    } else {
      buff = new byte[BUFF_SIZE];
    }

    while ((readLen = randomAccessFile.read(buff)) != -1) {
      outputStream.write(buff, 0, readLen);

      progressMonitor.updateWorkCompleted(readLen);
      if (progressMonitor.isCancelAllTasks()) {
        progressMonitor.setResult(ProgressMonitor.Result.CANCELLED);
        return;
      }

      bytesRead += readLen;

      if (bytesRead == bytesToRead) {
        break;
      } else if (bytesRead + buff.length > bytesToRead) {
        buff = new byte[(int) (bytesToRead - bytesRead)];
      }
    }

  } catch (IOException e) {
    throw new ZipException(e);
  }
}