| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.io.Closeable;
|
| 6 | import java.io.File;
|
| 7 | import java.io.IOException;
|
| 8 | import java.io.UncheckedIOException;
|
| 9 | import java.nio.charset.StandardCharsets;
|
| 10 | import java.nio.file.FileSystem;
|
| 11 | import java.nio.file.Files;
|
| 12 | import java.nio.file.Path;
|
| 13 | import java.util.stream.Collectors;
|
| 14 | import java.util.stream.Stream;
|
| 15 | import org.slf4j.Logger;
|
| 16 |
|
| 17 | public class FileZipper implements Closeable {
|
| 18 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 19 | private final Path outputFile;
|
| 20 | private final Path tempFile;
|
| 21 | private final FileSystem fs;
|
| 22 |
|
| 23 | public FileZipper(final Path outputFile) {
|
| 24 | this.outputFile = outputFile;
|
| 25 | this.tempFile = outputFile.resolveSibling(outputFile.getFileName().toString() + "_tmp");
|
| 26 |
|
| 27 | try {
|
| 28 | this.fs = Util.ZIP_FILE_SYSTEM_PROVIDER.newFileSystem(this.tempFile, ImmutableMap.of("create", "true"));
|
| 29 | } catch (IOException e) {
|
| 30 | throw new UncheckedIOException(e);
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | public void add(final Path destinationRelativePath, final String content) {
|
| 35 | try {
|
| 36 | Path root = this.fs.getPath(File.separator);
|
| 37 | Path path = root.resolve(destinationRelativePath.toString());
|
| 38 | Files.createDirectories(path.getParent());
|
| 39 | Files.write(path, content.getBytes(StandardCharsets.UTF_8));
|
| 40 | } catch (IOException e) {
|
| 41 | throw new UncheckedIOException(e);
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | public void add(final Path destinationRelativePath, final File file) {
|
| 46 | try {
|
| 47 | Path root = this.fs.getPath(File.separator);
|
| 48 | Path path = root.resolve(destinationRelativePath.toString());
|
| 49 | Files.createDirectories(path.getParent());
|
| 50 | Files.copy(file.toPath(), path);
|
| 51 | } catch (IOException e) {
|
| 52 | throw new UncheckedIOException(e);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | public void add(final Path path) {
|
| 57 | try {
|
| 58 | Path root = this.fs.getPath(File.separator);
|
| 59 | if (Files.isRegularFile(path)) {
|
| 60 | Path targetFile = root.resolve(path.getParent().relativize(path).toString());
|
| 61 | Files.copy(targetFile, path);
|
| 62 | } else {
|
| 63 | try (Stream<Path> sourceFiles = Files.find(path, Integer.MAX_VALUE, (p, a) -> a.isRegularFile())) {
|
| 64 | for (Path sourceFile : sourceFiles.collect(Collectors.toList())) {
|
| 65 | Path targetFile = root.resolve(path.relativize(sourceFile).toString());
|
| 66 | Files.createDirectories(targetFile.getParent());
|
| 67 | Files.copy(sourceFile, targetFile);
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 | } catch (IOException e) {
|
| 72 | throw new UncheckedIOException(e);
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void close() {
|
| 78 | try {
|
| 79 | this.fs.close();
|
| 80 | Files.move(this.tempFile, this.outputFile);
|
| 81 | LOGGER.info("Compressed to {}", this.outputFile);
|
| 82 | } catch (IOException e) {
|
| 83 | throw new UncheckedIOException(e);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|