Uses 204K of libraries. Click here for Pure Java version (5541L/31K).
// bit slow but works !include once #1034352 // zip4j import java.nio.charset.Charset; srecord noeq Zip4j_CopySubSetOfZipFile_workingBackup(File zipIn, File zipOut, IPred<S> fileFilter) { delegate ProgressMonitor to net.lingala.zip4j.progress. ZipModel zipModel; new HeaderWriter headerWriter; new ProgressMonitor progressMonitor; int bufferSize = InternalZipConstants.BUFF_SIZE; Charset charset; run { try (var randomAccessFile = new RandomAccessFile(zipIn, RandomAccessFileMode.READ.getValue())) { zipModel = new HeaderReader().readAllHeaders(randomAccessFile, buildConfig()); zipModel.setZipFile(zipIn); } temp outputStream = new SplitOutputStream(zipOut); temp inputStream = new RandomAccessFile(zipModel.getZipFile(), RandomAccessFileMode.READ.getValue()); long currentFileCopyPointer = 0; L<FileHeader> sortedFileHeaders = sortedByComparator(zipModel.getCentralDirectory().getFileHeaders(), (o1, o2) -> { if (eq(o1.getFileName(), o2.getFileName())) ret 0; ret cmp(o1.getOffsetLocalHeader(), o2.getOffsetLocalHeader()); }); int count = 0; for (fileHeader : sortedFileHeaders) { long fpIn = inputStream.getFilePointer(); long fp = outputStream.getFilePointer(); long lengthOfCurrentEntry = getOffsetOfNextEntry(sortedFileHeaders, fileHeader, zipModel) - fp; bool shouldCopy = fileFilter.get(fileHeader.getFileName()); if (count++ < 10) printVars(+fpIn, +fp, +lengthOfCurrentEntry, +shouldCopy); if (!shouldCopy) { updateHeaders(sortedFileHeaders, fileHeader, lengthOfCurrentEntry); if (!zipModel.getCentralDirectory().getFileHeaders().remove(fileHeader)) { throw new ZipException("Could not remove entry from list of central directory headers"); } currentFileCopyPointer += lengthOfCurrentEntry; } else { // copy complete entry without any changes currentFileCopyPointer += copyFile(inputStream, outputStream, currentFileCopyPointer, lengthOfCurrentEntry, progressMonitor, buildConfig().getBufferSize()); } } headerWriter.finalizeZipFile(zipModel, outputStream, buildConfig().getCharset()); } long getOffsetOfNextEntry(L<FileHeader> sortedFileHeaders, FileHeader fileHeader, ZipModel zipModel) throws ZipException { int indexOfFileHeader = getIndexOfFileHeader(sortedFileHeaders, fileHeader); if (indexOfFileHeader == sortedFileHeaders.size() - 1) ret HeaderUtil.getOffsetStartOfCentralDirectory(zipModel); else ret sortedFileHeaders.get(indexOfFileHeader + 1).getOffsetLocalHeader(); } long copyFile(RandomAccessFile randomAccessFile, OutputStream outputStream, long start, long length, ProgressMonitor progressMonitor, int bufferSize) throws IOException { net.lingala.zip4j.util.FileUtils.copyFile(randomAccessFile, outputStream, start, start + length, progressMonitor, bufferSize); ret length; } private void updateHeaders(List<FileHeader> sortedFileHeaders, FileHeader fileHeaderThatWasRemoved, long offsetToSubtract) throws ZipException { updateOffsetsForAllSubsequentFileHeaders(sortedFileHeaders, zipModel, fileHeaderThatWasRemoved, negate(offsetToSubtract)); EndOfCentralDirectoryRecord endOfCentralDirectoryRecord = zipModel.getEndOfCentralDirectoryRecord(); endOfCentralDirectoryRecord.setOffsetOfStartOfCentralDirectory( endOfCentralDirectoryRecord.getOffsetOfStartOfCentralDirectory() - offsetToSubtract); endOfCentralDirectoryRecord.setTotalNumberOfEntriesInCentralDirectory( endOfCentralDirectoryRecord.getTotalNumberOfEntriesInCentralDirectory() - 1); if (endOfCentralDirectoryRecord.getTotalNumberOfEntriesInCentralDirectoryOnThisDisk() > 0) { endOfCentralDirectoryRecord.setTotalNumberOfEntriesInCentralDirectoryOnThisDisk( endOfCentralDirectoryRecord.getTotalNumberOfEntriesInCentralDirectoryOnThisDisk() - 1); } if (zipModel.isZip64Format()) { zipModel.getZip64EndOfCentralDirectoryRecord().setOffsetStartCentralDirectoryWRTStartDiskNumber( zipModel.getZip64EndOfCentralDirectoryRecord().getOffsetStartCentralDirectoryWRTStartDiskNumber() - offsetToSubtract); zipModel.getZip64EndOfCentralDirectoryRecord().setTotalNumberOfEntriesInCentralDirectoryOnThisDisk( zipModel.getZip64EndOfCentralDirectoryRecord().getTotalNumberOfEntriesInCentralDirectory() - 1); zipModel.getZip64EndOfCentralDirectoryLocator().setOffsetZip64EndOfCentralDirectoryRecord( zipModel.getZip64EndOfCentralDirectoryLocator().getOffsetZip64EndOfCentralDirectoryRecord() - offsetToSubtract); } } private long negate(long val) { if (val == Long.MIN_VALUE) { throw new ArithmeticException("long overflow"); } return -val; } Zip4jConfig buildConfig() { return new Zip4jConfig(charset, bufferSize); } int getIndexOfFileHeader(List<FileHeader> allFileHeaders, FileHeader fileHeaderForIndex) throws ZipException { for (int i = 0; i < allFileHeaders.size(); i++) { FileHeader fileHeader = allFileHeaders.get(i); if (fileHeader.equals(fileHeaderForIndex)) { return i; } } throw new ZipException("Could not find file header in list of central directory file headers"); } void updateOffsetsForAllSubsequentFileHeaders(List<FileHeader> sortedFileHeaders, ZipModel zipModel, FileHeader fileHeaderModified, long offsetToAdd) throws ZipException { int indexOfFileHeader = getIndexOfFileHeader(sortedFileHeaders, fileHeaderModified); if (indexOfFileHeader == -1) { throw new ZipException("Could not locate modified file header in zipModel"); } for (int i = indexOfFileHeader + 1; i < sortedFileHeaders.size(); i++) { FileHeader fileHeaderToUpdate = sortedFileHeaders.get(i); fileHeaderToUpdate.setOffsetLocalHeader(fileHeaderToUpdate.getOffsetLocalHeader() + offsetToAdd); if (zipModel.isZip64Format() && fileHeaderToUpdate.getZip64ExtendedInfo() != null && fileHeaderToUpdate.getZip64ExtendedInfo().getOffsetLocalHeader() != -1) { fileHeaderToUpdate.getZip64ExtendedInfo().setOffsetLocalHeader( fileHeaderToUpdate.getZip64ExtendedInfo().getOffsetLocalHeader() + offsetToAdd ); } } } }
Began life as a copy of #1034351
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034354 |
Snippet name: | Zip4j_CopySubSetOfZipFile_workingBackup |
Eternal ID of this version: | #1034354/6 |
Text MD5: | f2d6f6e9bb0c5f6b1ed32f80e55cf9c6 |
Transpilation MD5: | 71ad3912e50c9f4528d47a28aa27c420 |
Author: | stefan |
Category: | javax / io |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-02-03 06:02:21 |
Source code size: | 6575 bytes / 148 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 169 / 254 |
Version history: | 5 change(s) |
Referenced in: | [show references] |