-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Reimplemented copyZipWithoutEmptyDirectories to remove nested empty directories * - Now removes empty directories by calling copyZipWithoutEmptyDirectories in Zip.scala, mirroring the behavior of StandaloneJarProcessor.java - Reimplemented copyZipWithoutEmptyDirectories to remove nested empty directories, and added a test for it. - Update SHA of shading tests of bytebuddy. A before and after diff of unzip -l shows no change in any file paths, timestamps and sizes, except for the two empty directories that get removed as expected: 7a8,9 > 0 01-03-1980 00:00 net/ > 0 01-03-1980 00:00 net/bytebuddy/ 93c95 < 733298 88 files --- > 733298 90 files * - Adding setCompressedSize(-1) in copyZipWithoutEmptyDirectories() makes SHAs for shading tests consistent across JDK versions 8, 11 and 21. * - Added back missing BufferedOutputStream in copyZipWithoutEmptyDirectories
- Loading branch information
1 parent
38ed500
commit 519cc62
Showing
4 changed files
with
104 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
jarjar/src/test/java/com/eed3si9n/jarjar/util/IOUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.eed3si9n.jarjar.util; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.stream.Stream; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
public class IOUtilTest { | ||
@Test | ||
public void testCopyZipWithoutEmptyDirectories() throws IOException { | ||
// create a temporary directory tree with a few empty files and empty directories | ||
Path tree = Files.createTempDirectory("tree"); | ||
Path zips = Files.createTempDirectory("zips"); | ||
|
||
// Create a zip with some empty directories | ||
Path a = Files.createDirectory(Paths.get(tree.toString(), "a")); | ||
Files.createFile(Paths.get(a.toString(), "a.txt")); | ||
Files.createDirectory(Paths.get(tree.toString(), "b")); | ||
Path c = Files.createDirectory(Paths.get(tree.toString(), "c")); | ||
Files.createDirectory(Paths.get(c.toString(), "d")); | ||
File inputZipFile = Paths.get(zips.toString(), "input.zip").toFile(); | ||
zipDirectory(tree, inputZipFile); | ||
|
||
File outputZipFile = Paths.get(zips.toString(), "output.zip").toFile(); | ||
IoUtil.copyZipWithoutEmptyDirectories(inputZipFile, outputZipFile); | ||
try (ZipFile outputZip = new ZipFile(outputZipFile)) { | ||
Assert.assertNotNull(outputZip.getEntry("a/a.txt")); | ||
Assert.assertNotNull(outputZip.getEntry("a/")); | ||
Assert.assertNull(outputZip.getEntry("b/")); | ||
Assert.assertNull(outputZip.getEntry("c/")); | ||
Assert.assertNull(outputZip.getEntry("c/d/")); | ||
} | ||
} | ||
|
||
private static void zipDirectory(Path sourceDir, File zipFile) throws IOException { | ||
try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); | ||
Stream<Path> paths = Files.walk(sourceDir) | ||
) { | ||
paths.forEach(path -> { | ||
String name = sourceDir.relativize(path).toString(); | ||
if (Files.isDirectory(path)) { | ||
name = name + "/"; | ||
} | ||
ZipEntry zipEntry = new ZipEntry(name); | ||
try { | ||
zipOutputStream.putNextEntry(zipEntry); | ||
if (!Files.isDirectory(path)) { | ||
Files.copy(path, zipOutputStream); | ||
} | ||
zipOutputStream.closeEntry(); | ||
} catch (IOException e) { | ||
throw new RuntimeIOException(e); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
||
} |