如何在Java中将zip文件转换为字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24927932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:47:48 来源:igfitidea点击:
How to convert zip file into bytes in Java
提问by user3278732
how can I convert a zip file into bytes?
如何将 zip 文件转换为字节?
byte[] ba; InputStream is = new ByteArrayInputStream(ba); InputStream zis = new ZipInputStream(is);
byte[] ba; InputStream is = new ByteArrayInputStream(ba); InputStream zis = new ZipInputStream(is);
采纳答案by Thilo
You can read a file from disk into a byte[]
using
您可以将文件从磁盘读入byte[]
using
byte[] ba = java.nio.file.Files.readAllBytes(filePath);
This is available from Java 7.
这在 Java 7 中可用。
回答by MadProgrammer
The basic principle is to feed the InputStream
into the OutputStream
, for example...
其基本原理是喂InputStream
到OutputStream
,例如...
byte bytes[] = null;
try (FileInputStream fis = new FileInputStream(new File("..."))) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int read = -1;
while ((read = fis.read(buffer)) != -1) {
baos.write(buffer, 0, read);
}
bytes = baos.toByteArray();
}
} catch (IOException exp) {
exp.printStackTrace();
}