Java将byte流转换成zip文件_如何将字节数组转换为ZIP文件

  • Post author:
  • Post category:java


我正在尝试将字节数组转换为ZIP文件。我使用以下代码获取字节:

byte[] originalContentBytes= new Verification().readBytesFromAFile(new File(“E://file.zip”));

private byte[] readBytesFromAFile(File file) {

int start = 0;

int length = 1024;

int offset = -1;

byte[] buffer = new byte[length];

try {

//convert the file content into a byte array

FileInputStream fileInuptStream = new FileInputStream(file);

BufferedInputStream bufferedInputStream = new BufferedInputStream(

fileInuptStream);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

while ((offset = bufferedInputStream.read(buffer, start, length)) != -1) {

byteArrayOutputStream.write(buffer, start, offset);

}

bufferedInputStream.close();

byteArrayOutputStream.flush();

buffer = byteArrayOutputStream.toByteArray();

byteArrayOutputStream.close();

} catch (FileNotFoundException fileNotFoundException) {

fileNotFoundException.printStackTrace();

} catch (IOException ioException) {

ioException.printStackTrace();

}

return buffer;

}

但是我现在的问题是将字节数组转换回ZIP文件-如何完成?

注意:指定的ZIP包含两个文件。



版权声明:本文为weixin_39581719原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。