Skip to content

Commit

Permalink
[core] use a different mechanism for unzipping files. Fixes #786
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Mar 1, 2022
1 parent b658a47 commit c94a867
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
Expand Down Expand Up @@ -59,6 +60,7 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -79,6 +81,7 @@
import static org.jreleaser.util.FileType.TBZ2;
import static org.jreleaser.util.FileType.TGZ;
import static org.jreleaser.util.FileType.TXZ;
import static org.jreleaser.util.FileType.ZIP;
import static org.jreleaser.util.StringUtils.getFilename;
import static org.jreleaser.util.StringUtils.isNotBlank;

Expand Down Expand Up @@ -244,6 +247,13 @@ public static void unpackArchive(Path src, Path dest, boolean removeRootEntry) t
deleteFiles(dest, true);
File destinationDir = dest.toFile();

if (filename.endsWith(ZIP.extension())) {
try (ZipFile zipFile = new ZipFile(src.toFile())) {
unpackArchive(removeRootEntry ? filename + "/" : "", destinationDir, zipFile);
}
return;
}

try (InputStream fi = Files.newInputStream(src);
InputStream bi = new BufferedInputStream(fi);
ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(bi)) {
Expand Down Expand Up @@ -337,6 +347,50 @@ private static void unpackArchive(String basename, File destinationDir, ArchiveI
}
}

private static void unpackArchive(String basename, File destinationDir, ZipFile zipFile) throws IOException {
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
if (!zipFile.canReadEntryData(entry)) {
// log something?
continue;
}

String entryName = entry.getName();
if (isNotBlank(basename) && entryName.startsWith(basename) && entryName.length() > basename.length() + 1) {
entryName = entryName.substring(basename.length());
}

File file = new File(destinationDir, entryName);
String destDirPath = destinationDir.getCanonicalPath();
String destFilePath = file.getCanonicalPath();
if (!destFilePath.startsWith(destDirPath + File.separator)) {
throw new IOException(RB.$("ERROR_files_unpack_outside_target", entry.getName()));
}

if (entry.isDirectory()) {
if (!file.isDirectory() && !file.mkdirs()) {
throw new IOException(RB.$("ERROR_files_unpack_fail_dir", file));
}
} else {
File parent = file.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
throw new IOException(RB.$("ERROR_files_unpack_fail_dir", parent));
}

if (entry.isUnixSymlink()) {
Files.createSymbolicLink(file.toPath(), Paths.get(zipFile.getUnixSymlink(entry)));
} else {
try (OutputStream o = Files.newOutputStream(file.toPath())) {
IOUtils.copy(zipFile.getInputStream(entry), o);
Files.setLastModifiedTime(file.toPath(), FileTime.from(entry.getLastModifiedDate().toInstant()));
chmod(file, getEntryMode(entry, file));
}
}
}
}
}

private static boolean isSymbolicLink(ArchiveEntry entry) {
if (entry instanceof ZipArchiveEntry) {
return ((ZipArchiveEntry) entry).isUnixSymlink();
Expand Down

0 comments on commit c94a867

Please sign in to comment.