|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 |
|
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.StandardOpenOption; |
| 10 | +import java.util.zip.ZipEntry; |
| 11 | +import java.util.zip.ZipOutputStream; |
5 | 12 | import javasabr.rlib.io.util.FileUtils; |
| 13 | +import org.assertj.core.api.Assertions; |
6 | 14 | import org.junit.jupiter.api.Test; |
7 | 15 |
|
8 | 16 | /** |
@@ -74,4 +82,41 @@ void shouldCheckExistingExtension() { |
74 | 82 | assertThat(FileUtils.hasExtension(path6)).isFalse(); |
75 | 83 | assertThat(FileUtils.hasExtension(path7)).isFalse(); |
76 | 84 | } |
| 85 | + |
| 86 | + @Test |
| 87 | + void shouldUnzipFileCorrectly() throws IOException { |
| 88 | + // given: |
| 89 | + Path zipFile = Files.createTempFile("test-archive", ".zip"); |
| 90 | + |
| 91 | + try (var zout = new ZipOutputStream(Files.newOutputStream(zipFile, StandardOpenOption.CREATE))) { |
| 92 | + zout.putNextEntry(new ZipEntry("fileA.txt")); |
| 93 | + zout.write("test text".getBytes(StandardCharsets.UTF_8)); |
| 94 | + |
| 95 | + zout.putNextEntry(new ZipEntry("../fileB.txt")); |
| 96 | + zout.write("test text 2".getBytes(StandardCharsets.UTF_8)); |
| 97 | + |
| 98 | + ZipEntry dirAEntry = new ZipEntry("dir_a/"); |
| 99 | + dirAEntry.setMethod(ZipEntry.STORED); |
| 100 | + dirAEntry.setSize(0); |
| 101 | + dirAEntry.setCrc(0); |
| 102 | + zout.putNextEntry(dirAEntry); |
| 103 | + |
| 104 | + zout.putNextEntry(new ZipEntry("dir_a/fileC.txt")); |
| 105 | + zout.write("test text 3".getBytes(StandardCharsets.UTF_8)); |
| 106 | + |
| 107 | + zout.putNextEntry(new ZipEntry("dir_a/../fileD.txt")); |
| 108 | + zout.write("test text 4".getBytes(StandardCharsets.UTF_8)); |
| 109 | + |
| 110 | + zout.putNextEntry(new ZipEntry("dir_a/../../../fileE.txt")); |
| 111 | + zout.write("test text 5".getBytes(StandardCharsets.UTF_8)); |
| 112 | + } |
| 113 | + |
| 114 | + Path outputDir = Files.createTempDirectory("test-unzip"); |
| 115 | + |
| 116 | + // when: |
| 117 | + int unpackedFiles = FileUtils.unzip(outputDir, zipFile); |
| 118 | + |
| 119 | + // then: |
| 120 | + assertThat(unpackedFiles).isEqualTo(3); |
| 121 | + } |
77 | 122 | } |
0 commit comments