@@ -9,6 +9,7 @@ import org.apache.commons.compress.utils.BoundedInputStream;
|
|||||||
import org.jackhuang.hmcl.util.io.FileUtils;
|
import org.jackhuang.hmcl.util.io.FileUtils;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
import java.util.zip.Inflater;
|
import java.util.zip.Inflater;
|
||||||
import java.util.zip.InflaterInputStream;
|
import java.util.zip.InflaterInputStream;
|
||||||
@@ -17,8 +18,22 @@ public enum NBTFileType {
|
|||||||
COMPRESSED("dat", "dat_old") {
|
COMPRESSED("dat", "dat_old") {
|
||||||
@Override
|
@Override
|
||||||
public Tag read(File file) throws IOException {
|
public Tag read(File file) throws IOException {
|
||||||
try (InputStream in = new GZIPInputStream(new FileInputStream(file))) {
|
try (BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(file))) {
|
||||||
Tag tag = NBTIO.readTag(in);
|
fileInputStream.mark(3);
|
||||||
|
byte[] header = new byte[3];
|
||||||
|
if (fileInputStream.read(header) < 3) {
|
||||||
|
throw new IOException("File is too small");
|
||||||
|
}
|
||||||
|
fileInputStream.reset();
|
||||||
|
|
||||||
|
InputStream input;
|
||||||
|
if (Arrays.equals(header, new byte[]{0x1f, (byte) 0x8b, 0x08})) {
|
||||||
|
input = new GZIPInputStream(fileInputStream);
|
||||||
|
} else {
|
||||||
|
input = fileInputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
Tag tag = NBTIO.readTag(input);
|
||||||
if (!(tag instanceof CompoundTag))
|
if (!(tag instanceof CompoundTag))
|
||||||
throw new IOException("Unexpected tag: " + tag);
|
throw new IOException("Unexpected tag: " + tag);
|
||||||
return tag;
|
return tag;
|
||||||
|
|||||||
Reference in New Issue
Block a user