File size: 3,252 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package net.minecraft.util;

import com.google.common.collect.ImmutableMap;
import com.mojang.logging.LogUtils;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.minecraft.Util;
import org.slf4j.Logger;

public class FileZipper implements Closeable {
    private static final Logger LOGGER = LogUtils.getLogger();
    private final Path outputFile;
    private final Path tempFile;
    private final FileSystem fs;

    public FileZipper(Path p_144697_) {
        this.outputFile = p_144697_;
        this.tempFile = p_144697_.resolveSibling(p_144697_.getFileName().toString() + "_tmp");

        try {
            this.fs = Util.ZIP_FILE_SYSTEM_PROVIDER.newFileSystem(this.tempFile, ImmutableMap.of("create", "true"));
        } catch (IOException ioexception) {
            throw new UncheckedIOException(ioexception);
        }
    }

    public void add(Path p_144704_, String p_144705_) {
        try {
            Path path = this.fs.getPath(File.separator);
            Path path1 = path.resolve(p_144704_.toString());
            Files.createDirectories(path1.getParent());
            Files.write(path1, p_144705_.getBytes(StandardCharsets.UTF_8));
        } catch (IOException ioexception) {
            throw new UncheckedIOException(ioexception);
        }
    }

    public void add(Path p_144701_, File p_144702_) {
        try {
            Path path = this.fs.getPath(File.separator);
            Path path1 = path.resolve(p_144701_.toString());
            Files.createDirectories(path1.getParent());
            Files.copy(p_144702_.toPath(), path1);
        } catch (IOException ioexception) {
            throw new UncheckedIOException(ioexception);
        }
    }

    public void add(Path p_144699_) {
        try {
            Path path = this.fs.getPath(File.separator);
            if (Files.isRegularFile(p_144699_)) {
                Path path3 = path.resolve(p_144699_.getParent().relativize(p_144699_).toString());
                Files.copy(path3, p_144699_);
            } else {
                try (Stream<Path> stream = Files.find(p_144699_, Integer.MAX_VALUE, (p_144707_, p_144708_) -> p_144708_.isRegularFile())) {
                    for (Path path1 : stream.collect(Collectors.toList())) {
                        Path path2 = path.resolve(p_144699_.relativize(path1).toString());
                        Files.createDirectories(path2.getParent());
                        Files.copy(path1, path2);
                    }
                }
            }
        } catch (IOException ioexception) {
            throw new UncheckedIOException(ioexception);
        }
    }

    @Override
    public void close() {
        try {
            this.fs.close();
            Files.move(this.tempFile, this.outputFile);
            LOGGER.info("Compressed to {}", this.outputFile);
        } catch (IOException ioexception) {
            throw new UncheckedIOException(ioexception);
        }
    }
}