【问题标题】:Weird issue with zip/gzip fileszip/gzip 文件的奇怪问题
【发布时间】:2019-01-27 01:33:38
【问题描述】:

以下是将字节保存到 .png 文件并压缩到给定名称文件夹中的程序。

 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();

当我使用 7 zip 手动打开它时,我看到以下文件夹结构 (c:\output\nameofzipfile.zip\nameofpng.png\nameofpng)。为什么会这样?我究竟做错了什么?据我了解,这应该是结构 (c:\output\nameofzipfile.zip\nameofpng.png)

【问题讨论】:

    标签: java zip png gzip


    【解决方案1】:

    使用以下代码

    byte[] 解码 = Base64.decodeBase64(contents); 尝试 (FileOutputStream fos = new FileOutputStream(zipFilePath + amazonOrderId + zipFileName)) { fos.write(解码); 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);
                        }
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      相关资源
      最近更新 更多