diff --git a/src/main/java/com/teamscale/upload/utils/FileSystemUtils.java b/src/main/java/com/teamscale/upload/utils/FileSystemUtils.java index 8022729..c299dc0 100644 --- a/src/main/java/com/teamscale/upload/utils/FileSystemUtils.java +++ b/src/main/java/com/teamscale/upload/utils/FileSystemUtils.java @@ -30,9 +30,6 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; @@ -94,22 +91,24 @@ public static String stripTarExtension(String fileName) { * Extract entries of a ZipFile to a directory when the ZipFile is created * externally. Note that this does not close the ZipFile, so the caller has to * take care of this. + *

+ * We use the apache commons ZipArchiveEntry instead of the java standard library + * ZipEntry since the apache commons variant preserves flags on files in the zip. + * In particular executable flags on shell scripts. */ public static List unzip(ZipFile zip, File targetDirectory) throws IOException { - Enumeration entries = zip.entries(); + Enumeration entries = zip.getEntries(); List extractedPaths = new ArrayList<>(); while (entries.hasMoreElements()) { - ZipEntry entry = entries.nextElement(); + ZipArchiveEntry entry = entries.nextElement(); if (entry.isDirectory()) { continue; } String fileName = entry.getName(); - try (InputStream entryStream = zip.getInputStream(entry)) { File file = new File(targetDirectory, fileName); ensureDirectoryExists(file.getParentFile()); - try (FileOutputStream outputStream = new FileOutputStream(file)) { copy(entryStream, outputStream); } diff --git a/src/main/java/com/teamscale/upload/utils/ZipFile.java b/src/main/java/com/teamscale/upload/utils/ZipFile.java new file mode 100644 index 0000000..1ad8930 --- /dev/null +++ b/src/main/java/com/teamscale/upload/utils/ZipFile.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) CQSE GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.teamscale.upload.utils; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; + +/** Wrapper around Apache Compress ZIP file that exposes the filename */ +public class ZipFile extends org.apache.commons.compress.archivers.zip.ZipFile { + + /** The filename */ + private String name; + + public ZipFile(File f) throws IOException { + super(f); + this.name = f.getName(); + } + + public ZipFile(String name) throws IOException { + super(name); + this.name = name; + } + + public ZipFile(String name, String encoding) throws IOException { + super(name, encoding); + this.name = name; + } + + public ZipFile(File f, String encoding) throws IOException { + super(f, encoding); + this.name = f.getName(); + } + + public ZipFile(File f, String encoding, boolean useUnicodeExtraFields) throws IOException { + super(f, encoding, useUnicodeExtraFields); + this.name = f.getName(); + } + + public ZipFile(File f, Charset charset) throws IOException { + this(f, charset.toString()); + } + + public String getName() { + return name; + } +} diff --git a/src/test/java/com/teamscale/upload/JLinkIntegrationTest.java b/src/test/java/com/teamscale/upload/JLinkIntegrationTest.java index 26ec4fd..933592e 100644 --- a/src/test/java/com/teamscale/upload/JLinkIntegrationTest.java +++ b/src/test/java/com/teamscale/upload/JLinkIntegrationTest.java @@ -3,9 +3,9 @@ import java.io.File; import java.io.IOException; import java.nio.file.Path; -import java.util.zip.ZipFile; import com.teamscale.upload.utils.FileSystemUtils; +import com.teamscale.upload.utils.ZipFile; import org.apache.commons.lang3.SystemUtils; import org.assertj.core.api.SoftAssertions; import org.jetbrains.annotations.NotNull;