Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Jan 7, 2024
1 parent d21f462 commit cefdb8c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cli-utils/src/main/java/net/neoforged/cliutils/JarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.stream.Stream;

public class JarUtils {
public static int getFileCountInZip(File path) throws IOException {
try (FileSystem fs = FileSystems.newFileSystem(path.toPath(), null); final Stream<Path> count = Files.find(fs.getPath("/"), Integer.MAX_VALUE, (p, basicFileAttributes) -> basicFileAttributes.isRegularFile())) {
final long c = count.count();
return c > (long) Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) c;
long c = 0;
try (FileSystem fs = FileSystems.newFileSystem(path.toPath(), null)) {
final Iterator<Path> roots = fs.getRootDirectories().iterator();
while (roots.hasNext()) {
try (final Stream<Path> count = Files.find(roots.next(), Integer.MAX_VALUE, (p, basicFileAttributes) -> basicFileAttributes.isRegularFile())) {
c += count.count();
}
}
}
return c > (long) Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) c;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.neoforged.cliutils.test;

import net.neoforged.cliutils.JarUtils;
import org.junit.jupiter.api.Test;

import java.io.File;

public class TestJarUtils {

@Test
void testCorrectCount() throws Exception {
assert JarUtils.getFileCountInZip(new File(getClass().getResource("/jarutils.zip").toURI())) == 3;
}
}
Binary file added cli-utils/src/test/resources/jarutils.zip
Binary file not shown.

0 comments on commit cefdb8c

Please sign in to comment.