Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add newFileNameMap for zipFile.extractAll #544

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
target/
*.iml
.DS_Store
out/
48 changes: 48 additions & 0 deletions src/main/java/net/lingala/zip4j/ZipFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,54 @@ public void extractAll(String destinationPath, UnzipParameters unzipParameters)
new ExtractAllFilesTaskParameters(destinationPath, buildConfig()));
}

/**
* Extracts all the files in the given zip file to the input destination path.
* If zip file does not exist or destination path is invalid then an
* exception is thrown.
* <br><br>
* newFileNameMap is used to rename the files in the zip file.
*
* @param destinationPath path to which the entries of the zip are to be extracted
* @param newFileNameMap map of old file name to new file name for the files to be extracted
* @throws ZipException when an issue occurs during extraction
*/
public void extractAll(String destinationPath, Map<String, String> newFileNameMap) throws ZipException {
extractAll(destinationPath, newFileNameMap, new UnzipParameters());
}

/**
* Extracts all entries in the zip file to the destination path considering the options defined in
* UnzipParameters
* <br><br>
* newFileNameMap is used to rename the files in the zip file.
*
* @param destinationPath path to which the entries of the zip are to be extracted
* @param newFileNameMap map of old file name to new file name for the files to be extracted
* @param unzipParameters parameters to be considered during extraction
* @throws ZipException when an issue occurs during extraction
*/
public void extractAll(String destinationPath, Map<String, String> newFileNameMap, UnzipParameters unzipParameters) throws ZipException {
if (!isStringNotNullAndNotEmpty(destinationPath)) {
throw new ZipException("output path is null or invalid");
}

if (!Zip4jUtil.createDirectoryIfNotExists(new File(destinationPath))) {
throw new ZipException("invalid output path");
}

if (zipModel == null) {
readZipInfo();
}

// Throw an exception if zipModel is still null
if (zipModel == null) {
throw new ZipException("Internal error occurred when extracting zip file");
}

new ExtractAllFilesTask(zipModel, password, newFileNameMap, unzipParameters, buildAsyncParameters()).execute(
new ExtractAllFilesTaskParameters(destinationPath, buildConfig()));
}

/**
* Extracts a specific file from the zip file to the destination path.
* If destination path is invalid, then this method throws an exception.
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/net/lingala/zip4j/tasks/ExtractAllFilesTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@
import net.lingala.zip4j.util.UnzipUtil;

import java.io.IOException;
import java.util.Map;

import static net.lingala.zip4j.headers.HeaderUtil.getTotalUncompressedSizeOfAllFileHeaders;

public class ExtractAllFilesTask extends AbstractExtractFileTask<ExtractAllFilesTaskParameters> {

private final char[] password;
private SplitFileInputStream splitInputStream;
private final Map<String, String> newFileNameMap;

public ExtractAllFilesTask(ZipModel zipModel, char[] password, UnzipParameters unzipParameters,
AsyncTaskParameters asyncTaskParameters) {
super(zipModel, unzipParameters, asyncTaskParameters);
this.password = password;
this.newFileNameMap = null;
}

public ExtractAllFilesTask(ZipModel zipModel, char[] password, Map<String, String> newFileNameMap, UnzipParameters unzipParameters,
AsyncTaskParameters asyncTaskParameters) {
super(zipModel, unzipParameters, asyncTaskParameters);
this.password = password;
this.newFileNameMap = newFileNameMap;
}

@Override
Expand All @@ -38,7 +48,7 @@ protected void executeTask(ExtractAllFilesTaskParameters taskParameters, Progres
splitInputStream.prepareExtractionForFileHeader(fileHeader);

byte[] readBuff = new byte[taskParameters.zip4jConfig.getBufferSize()];
extractFile(zipInputStream, fileHeader, taskParameters.outputPath, null, progressMonitor, readBuff);
extractFile(zipInputStream, fileHeader, taskParameters.outputPath, newFileNameMap != null ? newFileNameMap.get(fileHeader.getFileName()) : null, progressMonitor, readBuff);
verifyIfTaskIsCancelled();
}
} finally {
Expand Down