Skip to content

Commit 1e556cc

Browse files
committed
makeZip for file utils
1 parent d5ceed7 commit 1e556cc

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

file-utils/src/main/java/net/minecraftforge/util/file/FileUtils.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.Closeable;
1212
import java.io.File;
1313
import java.io.FileInputStream;
14+
import java.io.FileNotFoundException;
1415
import java.io.FileOutputStream;
1516
import java.io.IOException;
1617
import java.io.InputStream;
@@ -36,7 +37,9 @@
3637
import java.util.zip.ZipOutputStream;
3738

3839
// TODO remove Jar-related methods? some of them needs SrgUtils
39-
public class FileUtils {
40+
public final class FileUtils {
41+
private FileUtils() { }
42+
4043
public static final long ZIPTIME = 628041600000L;
4144
public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
4245

@@ -262,6 +265,17 @@ public static File makeJar(File classes, File sourcesDir, List<File> sources, Fi
262265
return jar;
263266
}
264267

268+
public static void makeZip(File inputDir, File zip) {
269+
try (var zos = new ZipOutputStream(new FileOutputStream(zip))) {
270+
for (var file : listFiles(inputDir)) {
271+
var entryName = inputDir.toPath().relativize(file.toPath()).toString();
272+
writeEntry(zos, new Info(entryName, file));
273+
}
274+
} catch (IOException e) {
275+
sneak(e);
276+
}
277+
}
278+
265279
private static void makeJarInternal(File dir, List<File> classes, JarOutputStream jos) throws IOException {
266280
for (var file : classes) {
267281
String entryName = file.getAbsoluteFile().toString().substring(dir.getAbsolutePath().length() + 1).replace('\\', '/');
@@ -275,7 +289,17 @@ private static void makeJarInternal(File dir, List<File> classes, JarOutputStrea
275289
}
276290
}
277291

278-
private record Info(String name, Supplier<InputStream> stream) {}
292+
private record Info(String name, Supplier<InputStream> stream) {
293+
private Info(String name, File file) {
294+
this(name, () -> {
295+
try {
296+
return new FileInputStream(file);
297+
} catch (FileNotFoundException e) {
298+
return sneak(e);
299+
}
300+
});
301+
}
302+
}
279303

280304
/** @see <a href="https://github.com/MinecraftForge/ForgeGradle/blob/efa70580314c88192486e4ab089f4b970d5b080c/src/common/java/net/minecraftforge/gradle/common/util/MinecraftRepo.java#L323-L327">MinecraftRepo.splitJar(...) in ForgeGradle 6</a> */
281305
public static void splitJar(File raw, File mappings, File output, boolean slim, boolean stable) throws IOException {

0 commit comments

Comments
 (0)