Java zip/gzip文件的奇怪问题
下面是一个程序,它将字节保存到.png文件中,并将其压缩到给定名称的文件夹中Java zip/gzip文件的奇怪问题,java,zip,png,gzip,Java,Zip,Png,Gzip,下面是一个程序,它将字节保存到.png文件中,并将其压缩到给定名称的文件夹中 byte[] decodedBytes = Base64.decodeBase64(contents); // System.out.println(new String(decodedBytes)); InputStream targetStream = new ByteArrayInputStream(decodedBytes);
byte[] decodedBytes = Base64.decodeBase64(contents);
// System.out.println(new String(decodedBytes));
InputStream targetStream = new ByteArrayInputStream(decodedBytes);
int count;
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilePath));
out.putNextEntry(new ZipEntry(pngFile));
byte[] b = new byte[1024];
while ((count = targetStream.read(b)) > 0) {
out.write(b, 0, count);
}
out.flush();
out.close();
targetStream.close();
当我使用7ZIP手动打开它时,我看到以下文件夹结构(c:\output\nameofzipfile.zip\nameofpng.png\nameofpng)。为什么会这样?我做错了什么?根据我的理解,这应该是结构(c:\output\nameofzipfile.zip\nameofpng.png)使用以下代码 字节[]decoded=Base64.decodeBase64(内容); try(FileOutputStream fos=newfileoutputstream(zipFilePath+amazonOrderId+zipFileName)){ fos.写入(解码); fos.close(); }
file = new File(destDirectory + amazonOrderId + pngFile);
if (file.exists()) {
file.delete();
}
try (OutputStream out = new FileOutputStream(destDirectory + amazonOrderId + pngFile)) {
try (InputStream in = new GZIPInputStream(
new FileInputStream(zipFilePath + amazonOrderId + zipFileName))) {
byte[] buffer = new byte[65536];
int noRead;
while ((noRead = in.read(buffer)) != -1) {
out.write(buffer, 0, noRead);
}
}
}