Skip to content

Commit

Permalink
TS-35786 Use apache commons zip since it preserves flags
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrhein committed Nov 21, 2023
1 parent 937f6a1 commit dcfa702
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/main/java/com/teamscale/upload/utils/FileSystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
* <p>
* 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<String> unzip(ZipFile zip, File targetDirectory) throws IOException {
Enumeration<? extends ZipEntry> entries = zip.entries();
Enumeration<? extends ZipArchiveEntry> entries = zip.getEntries();
List<String> 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);
}
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/teamscale/upload/utils/ZipFile.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dcfa702

Please sign in to comment.