Skip to content

Commit bb856df

Browse files
committed
zip can exclude some sub file / folder.
1 parent bb69470 commit bb856df

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

src/main/java/fr/formiko/utils/FLUFiles.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import java.nio.file.Files;
1010
import java.nio.file.Paths;
1111
import java.nio.file.StandardOpenOption;
12+
import java.util.ArrayList;
1213
import java.util.Arrays;
1314
import java.util.LinkedList;
1415
import java.util.List;
16+
import java.util.stream.Collectors;
1517
import java.util.zip.ZipEntry;
1618
import java.util.zip.ZipFile;
1719
import java.util.zip.ZipInputStream;
@@ -71,7 +73,10 @@ private static void setDownloadingMessage(String message) {
7173
public static List<String> listFiles(String path) { return internal.listFiles(path); }
7274
public static List<String> listFilesRecursively(String path) { return internal.listFilesRecursively(path); }
7375

74-
public static boolean zip(String source, String destination) { return internal.zip(source, destination); }
76+
public static boolean zip(String source, String destination) { return zip(source, destination, new ArrayList<>()); }
77+
public static boolean zip(String source, String destination, List<String> excluded) {
78+
return internal.zip(source, destination, excluded);
79+
}
7580
/**
7681
* @param directoryInsideZipToGet If it's a/b/ then b/ will be created. To have only b content, use a/b/*
7782
*/
@@ -94,6 +99,8 @@ public static boolean downloadAndUnzip(String url, String destination, String di
9499

95100
public static boolean openWebLinkInBrowser(String url) { return false; }
96101

102+
public static String toStandardPath(String path) { return path.replace('\\', '/'); }
103+
97104
// Internal class to hide implementation
98105
private static class FLUFilesInternal {
99106
private FLUFilesInternal() {} // hide constructor
@@ -291,16 +298,25 @@ private List<String> listFilesRecursively(String path) {
291298
}
292299
}
293300

294-
private boolean zip(String source, String destination) {
301+
private boolean zip(String sourceIn, String destination, List<String> excluded) {
302+
final String source = toStandardPath(sourceIn);
303+
destination = toStandardPath(destination);
304+
excluded = excluded.stream().map(FLUFiles::toStandardPath).map(s -> FLUStrings.addAtTheBeginningIfNeeded(s, source)).distinct()
305+
.collect(Collectors.toCollection(ArrayList::new));
306+
excluded.add(destination);
307+
308+
System.out.println(excluded);
309+
295310
if (isAValidePath(source) && isAValidePath(destination)) {
296311
destination = FLUStrings.addAtTheEndIfNeeded(destination, ".zip");
297312
createParents(destination);
313+
298314
File sourceFile = new File(source);
299315
if (!sourceFile.exists()) {
300316
return false;
301317
}
302318
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(Paths.get(destination)))) {
303-
zipFile(sourceFile, sourceFile.getName(), destination, zos);
319+
zipFile(sourceFile, sourceFile.getName(), destination, zos, excluded);
304320
return true;
305321
} catch (IOException e) {
306322
return false;
@@ -309,15 +325,21 @@ private boolean zip(String source, String destination) {
309325
return false;
310326
}
311327
}
312-
private void zipFile(File fileToZip, String fileName, String destination, ZipOutputStream zos) throws IOException {
328+
private void zipFile(File fileToZip, String fileName, String destination, ZipOutputStream zos, List<String> excluded)
329+
throws IOException {
330+
for (String exclude : excluded) {
331+
if (toStandardPath(fileToZip.getAbsolutePath() + (fileToZip.isDirectory() ? FILE_SEPARATOR : "")).equals(exclude)) {
332+
return;
333+
}
334+
}
313335
if (fileToZip.isDirectory()) {
314336
final String finalfileName = FLUStrings.addAtTheEndIfNeeded(fileName, FILE_SEPARATOR);
315337
zos.putNextEntry(new ZipEntry(finalfileName));
316338
zos.closeEntry();
317339
List<Boolean> allOk = Arrays.asList(true);
318340
Arrays.stream(fileToZip.listFiles()).forEach(file -> {
319341
try {
320-
zipFile(file, finalfileName + file.getName(), destination, zos);
342+
zipFile(file, finalfileName + file.getName(), destination, zos, excluded);
321343
} catch (IOException e) {
322344
allOk.set(0, false);
323345
}

src/test/java/fr/formiko/utils/FLUFilesTest.java

+36-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ void testListFiles(String path, boolean shouldWork, List<String> expectedFiles)
270270

271271
private static Stream<Arguments> testListFilesSource() {
272272
return Stream.of(
273-
Arguments.of(TEST_PATH, true, List.of("existingDir/", "existingFile.x", "existingFile2", "existingFile3", "existingFile4")),
273+
Arguments.of(TEST_PATH, true,
274+
List.of("existingDir/", "dir1/", "existingFile.x", "existingFile2", "existingFile3", "existingFile4")),
274275
Arguments.of(TEST_PATH + "existingDir/", true, List.of("subDir/")),
275276
Arguments.of(TEST_PATH + "existingDir/subDir/", true, List.of("existingFile.txt")), Arguments.of(null, false, null),
276277
Arguments.of(TEST_PATH + "unexistingDirectory", false, null));
@@ -295,6 +296,34 @@ private static Stream<Arguments> testZipSource() {
295296
Arguments.of("existingDir", false, null, null), Arguments.of(null, false, null, null));
296297
}
297298

299+
@ParameterizedTest
300+
@MethodSource("testZipWithExclusionSource")
301+
void testZipWithExclusion(String path, boolean shouldWork, String destination, String realDestination, List<String> exclusion,
302+
List<String> subItems) {
303+
assertEquals(shouldWork, FLUFiles.zip(path, destination, exclusion));
304+
if (shouldWork) {
305+
// unzip
306+
assertEquals(shouldWork, FLUFiles.unzip(destination, TEST_PATH_TEMPORARY + "TMP/", ""));
307+
// check if the files are here
308+
for (String s : subItems) {
309+
assertTrue(new File(TEST_PATH_TEMPORARY + "TMP/dir1/" + s).exists());
310+
assertTrue(FLUFiles.delete(TEST_PATH_TEMPORARY + "TMP/dir1/" + s));
311+
312+
}
313+
assertTrue(FLUFiles.delete(realDestination));
314+
}
315+
}
316+
317+
private static Stream<Arguments> testZipWithExclusionSource() {
318+
return Stream.of(
319+
Arguments.of(TEST_PATH + "existingDir/", true, TEST_PATH_TEMPORARY + "existingDir.zip",
320+
TEST_PATH_TEMPORARY + "existingDir.zip", List.of(), List.of()),
321+
Arguments.of(TEST_PATH + "dir1/", true, TEST_PATH_TEMPORARY + "dir1.zip", TEST_PATH_TEMPORARY + "dir1.zip",
322+
List.of("nonExistingFile.txt"), List.of("dir2/", "dir3/", "file4.md")),
323+
Arguments.of(TEST_PATH + "dir1/", true, TEST_PATH_TEMPORARY + "dir1.zip", TEST_PATH_TEMPORARY + "dir1.zip",
324+
List.of("dir2/"), List.of("dir3/", "file4.md")));
325+
}
326+
298327
@ParameterizedTest
299328
@MethodSource("testUnzipSource")
300329
void testUnzip(String pathToBeZip, boolean shouldWork, String zipedFile, String pathToDownloadIntoZip, String fileToCheck) {
@@ -389,6 +418,11 @@ public static void main(String[] args) {
389418

390419
clean();
391420
// FLUFiles.zip("../teavm/", "teavm.zip");
392-
FLUFiles.unzip("teavm.zip", TEST_PATH_TEMPORARY, "teavm/jso/*");
421+
// FLUFiles.unzip("teavm.zip", TEST_PATH_TEMPORARY, "teavm/jso/*");
422+
// FLUFiles.zip("C:\\Users\\Hydrolien\\git\\paper.1.21\\", "C:\\Users\\Hydrolien\\git\\paper.1.21\\saves\\1.zip",
423+
// List.of("saves/", "cache/"));
424+
// FLUFiles.zip("C:/Users/Hydrolien/git/paper.1.21/", "C:/Users/Hydrolien/git/paper.1.21/save/1.zip");
425+
FLUFiles.zip("C:/Users/Hydrolien/git/paper.1.21/", "C:/Users/Hydrolien/git/paper.1.21/saves/" + FLUTime.currentTime() + ".zip",
426+
List.of("saves/", "cache/"));
393427
}
394428
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ipnzéfl
2+
zgrebinoa
3+
rez bzn,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ipnzéfl
2+
zgrebinoa
3+
rez bzn,

src/test/resources/dir1/file4.md

Whitespace-only changes.

0 commit comments

Comments
 (0)