Uses 204K of libraries. Click here for Pure Java version (5494L/31K).
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 | print("isZip64"); |
62 | zipModel.getZip64EndOfCentralDirectoryRecord().setOffsetStartCentralDirectoryWRTStartDiskNumber( |
63 | zipModel.getZip64EndOfCentralDirectoryRecord().getOffsetStartCentralDirectoryWRTStartDiskNumber() + runningOffset); |
64 | |
65 | zipModel.getZip64EndOfCentralDirectoryRecord().setTotalNumberOfEntriesInCentralDirectoryOnThisDisk(l(filteredFileHeaders)); |
66 | |
67 | zipModel.getZip64EndOfCentralDirectoryLocator().setOffsetZip64EndOfCentralDirectoryRecord( |
68 | zipModel.getZip64EndOfCentralDirectoryLocator().getOffsetZip64EndOfCentralDirectoryRecord() + runningOffset); |
69 | } |
70 | |
71 | zipModel.getCentralDirectory().setFileHeaders(filteredFileHeaders); |
72 | headerWriter.finalizeZipFile(zipModel, outputStream, buildConfig().getCharset()); |
73 | } |
74 | |
75 | long getOffsetOfNextEntry(L<FileHeader> sortedFileHeaders, |
76 | int indexOfFileHeader, ZipModel zipModel) throws ZipException { |
77 | |
78 | if (indexOfFileHeader == sortedFileHeaders.size() - 1) |
79 | ret HeaderUtil.getOffsetStartOfCentralDirectory(zipModel); |
80 | else |
81 | ret sortedFileHeaders.get(indexOfFileHeader + 1).getOffsetLocalHeader(); |
82 | } |
83 | |
84 | long copyFile(RandomAccessFile randomAccessFile, |
85 | OutputStream outputStream, long start, long length, |
86 | ProgressMonitor progressMonitor, int bufferSize) throws IOException { |
87 | net.lingala.zip4j.util.FileUtils.copyFile(randomAccessFile, outputStream, start, start + length, progressMonitor, bufferSize); |
88 | ret length; |
89 | } |
90 | |
91 | Zip4jConfig buildConfig() { |
92 | return new Zip4jConfig(charset, bufferSize); |
93 | } |
94 | |
95 | void addOffsetToFileHeader(FileHeader fileHeader, long offsetToAdd) { |
96 | fileHeader.setOffsetLocalHeader(fileHeader.getOffsetLocalHeader() + offsetToAdd); |
97 | |
98 | if (zipModel.isZip64Format() |
99 | && fileHeader.getZip64ExtendedInfo() != null |
100 | && fileHeader.getZip64ExtendedInfo().getOffsetLocalHeader() != -1) |
101 | fileHeader.getZip64ExtendedInfo().setOffsetLocalHeader( |
102 | fileHeader.getZip64ExtendedInfo().getOffsetLocalHeader() + offsetToAdd |
103 | ); |
104 | } |
105 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034351 |
Snippet name: | Zip4j_CopySubSetOfZipFile |
Eternal ID of this version: | #1034351/36 |
Text MD5: | 2ee83e50763809935d4460515b4b103a |
Transpilation MD5: | 485fa997fd29c2778cdff422185b74d6 |
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:57 |
Source code size: | 4598 bytes / 105 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 242 / 488 |
Version history: | 35 change(s) |
Referenced in: | [show references] |