/
UnpackJob.java
388 lines (330 loc) · 17.4 KB
/
UnpackJob.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* This file is part of muCommander, http://www.mucommander.com
*
* muCommander is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* muCommander is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.mucommander.job.impl;
import com.mucommander.commons.file.*;
import com.mucommander.commons.file.archive.AbstractArchiveFile;
import com.mucommander.commons.file.archive.AbstractRWArchiveFile;
import com.mucommander.commons.file.archive.ArchiveEntry;
import com.mucommander.commons.file.archive.ArchiveEntryIterator;
import com.mucommander.commons.file.util.FileSet;
import com.mucommander.commons.file.util.PathUtils;
import com.mucommander.job.FileJobAction;
import com.mucommander.job.FileJobState;
import com.mucommander.text.Translator;
import com.mucommander.ui.action.ActionManager;
import com.mucommander.ui.action.impl.UnmarkAllAction;
import com.mucommander.ui.dialog.file.ProgressDialog;
import com.mucommander.ui.main.MainFrame;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
/**
* This job unpacks a set of archive files to a base destination folder. Archive entries are extracted in their natural
* order using {@link com.mucommander.commons.file.archive.AbstractArchiveFile#getEntryIterator()}, to traverse the archive only once
* and achieve optimal performance.
*
* @author Maxence Bernard
*/
public class UnpackJob extends AbstractCopyJob {
/** Archive entries to be unpacked */
protected List<ArchiveEntry> selectedEntries;
/** Depth of the folder in which the top entries are located. 0 is the highest depth (archive's root folder) */
protected int baseArchiveDepth;
/**
* Creates a new UnpackJob without starting it.
* <p>
* The base destination folder will be created if it doesn't exist.
* </p>
*
* @param progressDialog dialog which shows this job's progress
* @param mainFrame mainFrame this job has been triggered by
* @param files files which are going to be unpacked
* @param destFolder destination folder where the files will be copied
* @param fileExistsAction default action to be performed when a file already exists in the destination, see {@link com.mucommander.ui.dialog.file.FileCollisionDialog} for allowed values
*/
public UnpackJob(ProgressDialog progressDialog, MainFrame mainFrame, FileSet files, AbstractFile destFolder, int fileExistsAction) {
super(progressDialog, mainFrame, files, destFolder, null, fileExistsAction);
this.errorDialogTitle = Translator.get("unpack_dialog.error_title");
this.baseArchiveDepth = 0;
}
/**
* Creates a new UnpackJob without starting it.
*
* @param progressDialog dialog which shows this job's progress
* @param mainFrame mainFrame this job has been triggered by
* @param archiveFile the archive file which is going to be unpacked
* @param destFolder destination folder where the files will be copied
* @param newName the new filename in the destination folder, if <code>null</code> the original filename will be used
* @param fileExistsAction default action to be performed when a file already exists in the destination, see {@link com.mucommander.ui.dialog.file.FileCollisionDialog} for allowed values
* @param selectedEntries entries to be unpacked
* @param baseArchiveDepth depth of the folder in which the top entries are located. 0 is the highest depth (archive's root folder)
*/
public UnpackJob(ProgressDialog progressDialog, MainFrame mainFrame, AbstractArchiveFile archiveFile, int baseArchiveDepth, AbstractFile destFolder, String newName, int fileExistsAction, List<ArchiveEntry> selectedEntries) {
super(progressDialog, mainFrame, new FileSet(archiveFile.getParent(), archiveFile), destFolder, newName, fileExistsAction);
this.errorDialogTitle = Translator.get("unpack_dialog.error_title");
this.baseArchiveDepth = baseArchiveDepth;
this.selectedEntries = selectedEntries;
}
////////////////////////////////////
// TransferFileJob implementation //
////////////////////////////////////
@Override
protected void jobStarted() {
super.jobStarted();
// Create the base destination folder if it doesn't exist yet
if(!baseDestFolder.exists()) {
// Loop for retry
do {
try {
baseDestFolder.mkdir();
}
catch(IOException e) {
// Unable to create folder
int ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_create_folder", baseDestFolder.getName()));
// Retry loops
if(ret==FileJobAction.RETRY)
continue;
// Cancel or close dialog interrupts the job
interrupt();
// Skip continues
}
break;
} while(true);
}
}
/**
* Unpacks the given archive file. If the file is a directory, its children will be processed recursively.
* If the file is not an archive file nor a directory, it is not processed and <code>false</code> is returned.
*
* @param file the file to unpack
* @param recurseParams unused
* @return <code>true</code> if the file has been processed successfully
*/
@Override
protected boolean processFile(AbstractFile file, Object recurseParams) {
// Stop if interrupted
if (getState() == FileJobState.INTERRUPTED)
return false;
// Destination folder
AbstractFile destFolder = baseDestFolder;
// If the file is a directory, process its children recursively
if(file.isDirectory()) {
do { // Loop for retries
try {
// List files inside archive file (can throw an IOException)
AbstractFile[] archiveFiles = getCurrentFile().ls();
// Recurse on zip's contents
for(int j=0; j<archiveFiles.length && getState() != FileJobState.INTERRUPTED; j++) {
// Notify job that we're starting to process this file (needed for recursive calls to processFile)
nextFile(archiveFiles[j]);
// Recurse
processFile(archiveFiles[j], destFolder);
}
// Return true when complete
return true;
}
catch(IOException e) {
// File could not be uncompressed properly
int ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_read_file", getCurrentFilename()));
// Retry loops
if(ret==FileJobAction.RETRY)
continue;
// cancel, skip or close dialog will simply return false
return false;
}
} while(true);
}
// Abort if the file is neither an archive file nor a directory
if(!file.isArchive())
return false;
// 'Cast' the file as an archive file
AbstractArchiveFile archiveFile = file.getAncestor(AbstractArchiveFile.class);
String destSeparator = destFolder.getSeparator();
// Unpack the archive, copying entries one by one, in the iterator's order
try(ArchiveEntryIterator iterator = archiveFile.getEntryIterator()) {
ArchiveEntry entry;
while((entry = iterator.nextEntry())!=null && getState() != FileJobState.INTERRUPTED) {
String entryPath = entry.getPath();
boolean processEntry = false;
if(selectedEntries ==null) { // Entries are processed
processEntry = true;
}
else { // We need to determine if the entry should be processed or not
// Process this entry if the selectedEntries set contains this entry, or a parent of this entry
int nbSelectedEntries = selectedEntries.size();
for(int i=0; i<nbSelectedEntries; i++) {
ArchiveEntry selectedEntry = selectedEntries.get(i);
// Note: paths of directory entries must end with '/', so this compares whether
// selectedEntry is a parent of the current entry.
if(selectedEntry.isDirectory()) {
if(entryPath.startsWith(selectedEntry.getPath())) {
processEntry = true;
break;
// Note: we can't remove selectedEntryPath from the set, we still need it
}
}
else if(entryPath.equals(selectedEntry.getPath())) {
// If the (regular file) entry is in the set, remove it as we no longer need it (will speed up
// subsequent searches)
processEntry = true;
selectedEntries.remove(i);
break;
}
}
}
if(!processEntry)
continue;
DefaultMutableTreeNode entryNode = archiveFile.getArchiveEntryNode(entryPath);
if (entryNode != null) {
ArchiveEntry archiveEntry = (ArchiveEntry) entryNode.getUserObject();
if (archiveEntry.isSymbolicLink()) {
Files.createSymbolicLink(
FileSystems.getDefault().getPath(destFolder.getPath(), entry.getName()),
FileSystems.getDefault().getPath(entry.getLinkTarget()));
continue;
}
}
// Resolve the entry file
AbstractFile entryFile = archiveFile.getArchiveEntryFile(entryPath);
// Notify the job that we're starting to process this file
nextFile(entryFile);
// Figure out the destination file's path, relatively to the base destination folder
String relDestPath = baseArchiveDepth==0
?entry.getPath()
:PathUtils.removeLeadingFragments(entry.getPath(), "/", baseArchiveDepth);
if(newName!=null)
relDestPath = newName+(PathUtils.getDepth(relDestPath, "/")<=1?"":"/"+PathUtils.removeLeadingFragments(relDestPath, "/", 1));
if(!"/".equals(destSeparator))
relDestPath = relDestPath.replace("/", destSeparator);
// Create destination AbstractFile instance
AbstractFile destFile = destFolder.getChild(relDestPath);
// Check for ZipSlip (see https://snyk.io/research/zip-slip-vulnerability)
do {
if (destFolder.isParentOf(destFile))
break;
int ret = showErrorDialog(errorDialogTitle, Translator.get("unpack.entry_out_of_target_dir", destFile.getName()));
// Retry loops
if(ret==FileJobAction.RETRY)
continue;
// Cancel, skip or close dialog returns false
return false;
} while(true);
// Check if the file does not already exist in the destination
destFile = checkForCollision(entryFile, destFolder, destFile, false);
if (destFile == null) {
// A collision occurred and either the file was skipped, or the user cancelled the job
continue;
}
// It is noteworthy that the iterator returns entries in no particular order (consider it random).
// For that reason, we cannot assume that the parent directory of an entry will be processed
// before the entry itself.
// If the entry is a directory ...
if(entryFile.isDirectory()) {
// Create the directory in the destination, if it doesn't already exist
if(!(destFile.exists() && destFile.isDirectory())) {
// Loop for retry
do {
try {
// Use mkdirs() instead of mkdir() to create any parent folder that doesn't exist yet
destFile.mkdirs();
}
catch(IOException e) {
// Unable to create folder
int ret = showErrorDialog(errorDialogTitle, Translator.get("cannot_create_folder", entryFile.getName()));
// Retry loops
if(ret==FileJobAction.RETRY)
continue;
// Cancel or close dialog return false
return false;
// Skip continues
}
break;
} while(true);
}
}
// The entry is a regular file, copy it
else {
// Create the file's parent directory(s) if it doesn't already exist
AbstractFile destParentFile = destFile.getParent();
if(!destParentFile.exists()) {
// Use mkdirs() instead of mkdir() to create any parent folder that doesn't exist yet
destParentFile.mkdirs();
}
// The entry is wrapped in a ProxyFile to override #getInputStream() and delegate it to
// ArchiveFile#getEntryInputStream in order to take advantage of the ArchiveEntryIterator, which for
// some archive file implementations (such as TAR) can speed things by an order of magnitude.
if(!tryCopyFile(new ProxiedEntryFile(entryFile, entry, archiveFile, iterator), destFile, append, errorDialogTitle))
return false;
}
}
return true;
}
catch(IOException e) {
showErrorDialog(errorDialogTitle, Translator.get("cannot_read_file", archiveFile.getName()));
}
return false;
}
// This job modifies the base destination folder and its subfolders
@Override
protected boolean hasFolderChanged(AbstractFile folder) {
return baseDestFolder.isParentOf(folder);
}
////////////////////////
// Overridden methods //
////////////////////////
@Override
protected void jobCompleted() {
super.jobCompleted();
// If the destination files are located inside an archive, optimize the archive file
AbstractArchiveFile archiveFile = baseDestFolder.getParentArchive();
if(archiveFile!=null && archiveFile.isArchive() && archiveFile.isWritable())
optimizeArchive((AbstractRWArchiveFile)archiveFile);
// Unselect all files in the active table upon successful completion
if(selectedEntries!=null) {
ActionManager.performAction(UnmarkAllAction.Descriptor.ACTION_ID, getMainFrame());
}
}
@Override
public String getStatusString() {
if(isCheckingIntegrity())
return super.getStatusString();
if(isOptimizingArchive)
return Translator.get("optimizing_archive", archiveToOptimize.getName());
return Translator.get("unpack_dialog.unpacking_file", getCurrentFilename());
}
///////////////////
// Inner classes //
///////////////////
private static class ProxiedEntryFile extends ProxyFile {
private ArchiveEntry entry;
private AbstractArchiveFile archiveFile;
private ArchiveEntryIterator iterator;
public ProxiedEntryFile(AbstractFile entryFile, ArchiveEntry entry, AbstractArchiveFile archiveFile, ArchiveEntryIterator iterator) {
super(entryFile);
this.entry = entry;
this.archiveFile = archiveFile;
this.iterator = iterator;
}
@Override
public InputStream getInputStream() throws IOException {
return archiveFile.getEntryInputStream(entry, iterator);
}
}
}