Last active
June 1, 2017 03:20
-
-
Save Dereku/58bfc63aa47e2ac15be2741efb660a9d to your computer and use it in GitHub Desktop.
Распаковка pack.xz в файл. Наркоманы какие то.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Dependencies: org.tukaani.xz | |
/** | |
* Распаковка файла в файл | |
* | |
* @param input файл для распаковки | |
* @param output во что распаковывать | |
* @throws IOException | |
*/ | |
public static void unpackXZ(File input, File output) throws IOException { | |
unpackXZ(Files.readAllBytes(input.toPath()), output, false); | |
} | |
/** | |
* Распаковка InputStream в файл | |
* | |
* @param in InputStream для распаковки | |
* @param output ао что распаковывать | |
* @throws IOException | |
*/ | |
public static void unpackXZ(InputStream in, File output) throws IOException { | |
unpackXZ(sun.misc.IOUtils.readFully(in, -1, false), output, false); | |
} | |
/** | |
* Распаковка массива byte в файл | |
* | |
* @param data массив byte для распаковки | |
* @param output во что распаковывать | |
* @param saveChecksums сохранять ли хэшсуммы | |
* @throws IOException | |
*/ | |
public static void unpackXZ(byte[] data, File output, boolean saveChecksums) throws IOException { | |
byte[] decompressed; | |
//Создаём входящие потоки данных | |
try (ByteArrayInputStream bais = new ByteArrayInputStream(data); | |
XZInputStream xzis = new XZInputStream(bais)) { | |
//Читаем XZInputStream в массив byte. Метод что | |
//подсвечивается желательно заменить на другой. | |
decompressed = sun.misc.IOUtils.readFully(xzis, -1, false); | |
} | |
//Читаем размеры прочитанного массива и подписи | |
int x = decompressed.length; | |
int len = decompressed[x - 8] & 255 | (decompressed[x - 7] & 255) << 8 | |
| (decompressed[x - 6] & 255) << 16 | (decompressed[x - 5] & 255) << 24; | |
//Создаём входящий поток данных их того, что прочитали ранее минус | |
//размер подписей, создаём исходщий поток данных для записи | |
try (ByteArrayInputStream bais = new ByteArrayInputStream(Arrays.copyOfRange(decompressed, 0, x - len - 8)); | |
FileOutputStream fos = new FileOutputStream(output); | |
JarOutputStream jos = new JarOutputStream(fos)) { | |
//Распаковываем это всё дело в файл и были таковы. | |
Pack200.newUnpacker().unpack(bais, jos); | |
} | |
if (saveChecksums) { | |
byte[] checksums = Arrays.copyOfRange(decompressed, x - len - 8, x - 8); | |
File checksumsFile = new File(output.getParent(), "checksums.sha1"); | |
Files.write(checksumsFile.toPath(), checksums, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment