如何将zip文件转换为字节?
Run Code Online (Sandbox Code Playgroud)byte[] ba; InputStream is = new ByteArrayInputStream(ba); InputStream zis = new ZipInputStream(is);
您可以将文件从磁盘读取到byte[]使用中
byte[] ba = java.nio.file.Files.readAllBytes(filePath);
Run Code Online (Sandbox Code Playgroud)
这可以从Java 7获得.
其基本原理是喂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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9523 次 |
| 最近记录: |