Skip to content

Commit 1dcb44e

Browse files
ctruedenclaude
andcommitted
Fix the Downloads.unZip method
I lazily copy-pasted from unBZip2, resulting in a completely wrong implementation. Of course, we need to iterate the zip entries. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 95ca2cf commit 1dcb44e

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/main/java/org/apposed/appose/util/Downloads.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
3434
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
3535
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
36+
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
37+
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
3638
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
3739
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
3840
import org.apposed.appose.Appose;
@@ -50,7 +52,6 @@
5052
import java.net.URI;
5153
import java.net.URISyntaxException;
5254
import java.net.URL;
53-
import java.util.zip.ZipInputStream;
5455

5556
/**
5657
* Utility methods supporting download and unpacking of remote archives.
@@ -121,7 +122,7 @@ public static void unpack(final File inputFile, final File outputDir) throws Fil
121122
}
122123

123124
/**
124-
* Decompress a zip file into a new file.
125+
* Decompress a zip file into a directory.
125126
* @param source .zip file
126127
* @param destination
127128
* destination folder where the contents of the file are going to be decompressed
@@ -131,10 +132,32 @@ public static void unpack(final File inputFile, final File outputDir) throws Fil
131132
*/
132133
public static void unZip(File source, File destination) throws FileNotFoundException, IOException, InterruptedException {
133134
try (
134-
ZipInputStream input = new ZipInputStream(new BufferedInputStream(new FileInputStream(source)));
135-
FileOutputStream output = new FileOutputStream(destination);
135+
InputStream is = new FileInputStream(source);
136+
ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new BufferedInputStream(is));
136137
) {
137-
copy(input, output);
138+
ZipArchiveEntry entry = null;
139+
while ((entry = zipInputStream.getNextEntry()) != null) {
140+
final File outputFile = new File(destination, entry.getName());
141+
if (entry.isDirectory()) {
142+
if (!outputFile.exists()) {
143+
if (!outputFile.mkdirs()) {
144+
throw new IOException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
145+
}
146+
}
147+
} else {
148+
if (!outputFile.getParentFile().exists()) {
149+
if (!outputFile.getParentFile().mkdirs())
150+
throw new IOException("Failed to create directory " + outputFile.getParentFile().getAbsolutePath());
151+
}
152+
try (OutputStream outputFileStream = new FileOutputStream(outputFile)) {
153+
copy(zipInputStream, outputFileStream);
154+
}
155+
// Set executable permission if the entry had it
156+
if ((entry.getUnixMode() & 0100) != 0) {
157+
outputFile.setExecutable(true);
158+
}
159+
}
160+
}
138161
}
139162
}
140163

@@ -160,7 +183,7 @@ public static void unTar(final File inputFile, final File outputDir) throws File
160183
if (entry.isDirectory()) {
161184
if (!outputFile.exists()) {
162185
if (!outputFile.mkdirs()) {
163-
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
186+
throw new IOException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
164187
}
165188
}
166189
} else {
@@ -201,7 +224,7 @@ public static void unTarGz(final File inputFile, final File outputDir) throws Fi
201224
if (entry.isDirectory()) {
202225
if (!outputFile.exists()) {
203226
if (!outputFile.mkdirs()) {
204-
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
227+
throw new IOException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
205228
}
206229
}
207230
} else {

0 commit comments

Comments
 (0)