1 | !include once #1034352 // zip4j |
2 | |
3 | import java.nio.charset.Charset; |
4 | |
5 | srecord noeq Zip4j_CopySubSetOfZipFile(File zipIn, File zipOut, IPred<S> fileFilter) { |
6 | delegate ProgressMonitor to net.lingala.zip4j.progress. |
7 | |
8 | ZipModel zipModel; |
9 | new HeaderWriter headerWriter; |
10 | new ProgressMonitor progressMonitor; |
11 | int bufferSize = InternalZipConstants.BUFF_SIZE; |
12 | Charset charset; |
13 | |
14 | run { |
15 | try (var randomAccessFile = new RandomAccessFile(zipIn, RandomAccessFileMode.READ.getValue())) { |
16 | zipModel = new HeaderReader().readAllHeaders(randomAccessFile, buildConfig()); |
17 | zipModel.setZipFile(zipIn); |
18 | } |
19 | |
20 | temp outputStream = new SplitOutputStream(zipOut); |
21 | temp inputStream = new RandomAccessFile(zipModel.getZipFile(), RandomAccessFileMode.READ.getValue()); |
22 | |
23 | long currentFileCopyPointer = 0, runningOffset = 0; |
24 | L<FileHeader> sortedFileHeaders = sortedByComparator(zipModel.getCentralDirectory().getFileHeaders(), (o1, o2) -> { |
25 | if (eq(o1.getFileName(), o2.getFileName())) ret 0; |
26 | ret cmp(o1.getOffsetLocalHeader(), o2.getOffsetLocalHeader()); |
27 | }); |
28 | |
29 | int count = 0; |
30 | new L<FileHeader> filteredFileHeaders; |
31 | for iFileHeader over sortedFileHeaders: { |
32 | var fileHeader = sortedFileHeaders.get(iFileHeader); |
33 | var fileHeaderOffset = fileHeader.getOffsetLocalHeader(); |
34 | var nextOffset = getOffsetOfNextEntry(sortedFileHeaders, iFileHeader, zipModel); |
35 | long fp = outputStream.getFilePointer(); |
36 | long lengthOfCurrentEntry = nextOffset + runningOffset - fp; |
37 | |
38 | if (count++ < 10) |
39 | printVars(+fileHeaderOffset, +nextOffset, +runningOffset, +lengthOfCurrentEntry, +fp); |
40 | |
41 | if (!fileFilter.get(fileHeader.getFileName())) { |
42 | runningOffset -= lengthOfCurrentEntry; |
43 | currentFileCopyPointer += lengthOfCurrentEntry; |
44 | } else { |
45 | addOffsetToFileHeader(fileHeader, runningOffset); |
46 | filteredFileHeaders.add(fileHeader); |
47 | |
48 | // copy complete entry without any changes |
49 | currentFileCopyPointer += copyFile(inputStream, outputStream, |
50 | currentFileCopyPointer, |
51 | lengthOfCurrentEntry, progressMonitor, |
52 | buildConfig().getBufferSize()); |
53 | } |
54 | } |
55 | |
56 | var endOfCDR = zipModel.getEndOfCentralDirectoryRecord(); |
57 | endOfCDR.setTotalNumberOfEntriesInCentralDirectory(l(filteredFileHeaders)); |
58 | endOfCDR.setTotalNumberOfEntriesInCentralDirectoryOnThisDisk(l(filteredFileHeaders)); |
59 | |
60 | if (zipModel.isZip64Format()) { |
61 | zipModel.getZip64EndOfCentralDirectoryRecord().setOffsetStartCentralDirectoryWRTStartDiskNumber( |
62 | zipModel.getZip64EndOfCentralDirectoryRecord().getOffsetStartCentralDirectoryWRTStartDiskNumber() + runningOffset); |
63 | |
64 | zipModel.getZip64EndOfCentralDirectoryRecord().setTotalNumberOfEntriesInCentralDirectoryOnThisDisk(l(filteredFileHeaders)); |
65 | |
66 | zipModel.getZip64EndOfCentralDirectoryLocator().setOffsetZip64EndOfCentralDirectoryRecord( |
67 | zipModel.getZip64EndOfCentralDirectoryLocator().getOffsetZip64EndOfCentralDirectoryRecord() + runningOffset); |
68 | } |
69 | |
70 | zipModel.getCentralDirectory().setFileHeaders(filteredFileHeaders); |
71 | headerWriter.finalizeZipFile(zipModel, outputStream, buildConfig().getCharset()); |
72 | } |
73 | |
74 | long getOffsetOfNextEntry(L<FileHeader> sortedFileHeaders, |
75 | int indexOfFileHeader, ZipModel zipModel) throws ZipException { |
76 | |
77 | if (indexOfFileHeader == sortedFileHeaders.size() - 1) |
78 | ret HeaderUtil.getOffsetStartOfCentralDirectory(zipModel); |
79 | else |
80 | ret sortedFileHeaders.get(indexOfFileHeader + 1).getOffsetLocalHeader(); |
81 | } |
82 | |
83 | long copyFile(RandomAccessFile randomAccessFile, |
84 | OutputStream outputStream, long start, long length, |
85 | ProgressMonitor progressMonitor, int bufferSize) throws IOException { |
86 | net.lingala.zip4j.util.FileUtils.copyFile(randomAccessFile, outputStream, start, start + length, progressMonitor, bufferSize); |
87 | ret length; |
88 | } |
89 | |
90 | Zip4jConfig buildConfig() { |
91 | return new Zip4jConfig(charset, bufferSize); |
92 | } |
93 | |
94 | void addOffsetToFileHeader(FileHeader fileHeader, long offsetToAdd) { |
95 | fileHeader.setOffsetLocalHeader(fileHeader.getOffsetLocalHeader() + offsetToAdd); |
96 | |
97 | if (zipModel.isZip64Format() |
98 | && fileHeader.getZip64ExtendedInfo() != null |
99 | && fileHeader.getZip64ExtendedInfo().getOffsetLocalHeader() != -1) |
100 | fileHeader.getZip64ExtendedInfo().setOffsetLocalHeader( |
101 | fileHeader.getZip64ExtendedInfo().getOffsetLocalHeader() + offsetToAdd |
102 | ); |
103 | } |
104 | } |
Began life as a copy of #1034351
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034357 |
Snippet name: | Zip4j_CopySubSetOfZipFile working backup 3 (full speed) |
Eternal ID of this version: | #1034357/1 |
Text MD5: | a84eb02a0e1a4555f51603a329586652 |
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:26:00 |
Source code size: | 4573 bytes / 104 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 146 / 166 |
Referenced in: | [show references] |