下面列出了com.google.common.io.Files#asByteSink ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void unPackJar(final String jarFilePath, final File target) {
try (JarFile jarFile = new JarFile(URLUtil.removeProtocol(jarFilePath))) {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if (entry.isDirectory()) {
new File(target, entry.getName()).mkdirs();
} else {
File file = new File(target, entry.getName());
if (file.createNewFile()) {
try (InputStream inputStream = jarFile.getInputStream(entry)) {
ByteSink byteSink = Files.asByteSink(file);
byteSink.writeFrom(inputStream);
}
}
}
}
} catch (Exception e) {
logger.error("", "unpack jar error", e);
}
}
@Test
public void whenWriteUsingByteSink_thenWritten() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test.out");
final ByteSink sink = Files.asByteSink(file);
sink.write(expectedValue.getBytes());
final String result = Files.toString(file, Charsets.UTF_8);
assertEquals(expectedValue, result);
}
public static void writeFstToBinaryFile(Fst fst, File file) throws IOException {
ByteSink bs = Files.asByteSink(file);
try (ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(bs.openBufferedStream()))) {
writeFstToBinaryStream(fst, oos);
}
}