diff --git a/.gitignore b/.gitignore index d784d6a..5bae229 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ target/ *.iml .DS_Store +out/ \ No newline at end of file diff --git a/src/main/java/net/lingala/zip4j/ZipFile.java b/src/main/java/net/lingala/zip4j/ZipFile.java index 9c035d3..a31e4bc 100755 --- a/src/main/java/net/lingala/zip4j/ZipFile.java +++ b/src/main/java/net/lingala/zip4j/ZipFile.java @@ -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. + *

+ * 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 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 + *

+ * 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 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. diff --git a/src/main/java/net/lingala/zip4j/tasks/ExtractAllFilesTask.java b/src/main/java/net/lingala/zip4j/tasks/ExtractAllFilesTask.java index f57d518..584a97c 100644 --- a/src/main/java/net/lingala/zip4j/tasks/ExtractAllFilesTask.java +++ b/src/main/java/net/lingala/zip4j/tasks/ExtractAllFilesTask.java @@ -11,6 +11,7 @@ import net.lingala.zip4j.util.UnzipUtil; import java.io.IOException; +import java.util.Map; import static net.lingala.zip4j.headers.HeaderUtil.getTotalUncompressedSizeOfAllFileHeaders; @@ -18,11 +19,20 @@ public class ExtractAllFilesTask extends AbstractExtractFileTask 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 newFileNameMap, UnzipParameters unzipParameters, + AsyncTaskParameters asyncTaskParameters) { + super(zipModel, unzipParameters, asyncTaskParameters); + this.password = password; + this.newFileNameMap = newFileNameMap; } @Override @@ -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 {