java - 如何在java中将.zip压缩为.gz?

标签 java server zip gzipoutputstream

我想将普通文件压缩为 .zip,然后将其压缩为 gzip 文件,而不创建新文件。

例如,假设我有一个pdf文档doc.pdf,我必须得到的是:doc.pdf.zip.gz

我不想创建一个名为 doc.pdf.zip 的新文件,然后打开它并对其进行 gzip。

我正在与服务器合作,从浏览器获取文件并将其返回,这是我的功能:

 public void ZIPandGZIP(String fileName, OutputStream os, String header) throws FileNotFoundException
 {
    File file = new File(fileName);
    FileInputStream fis = new FileInputStream(file);
    byte[] data = new byte[(int) file.length()];
    DataOutputStream dos = new DataOutputStream(os);
    try {
        dos.writeBytes(header);
        ZipOutputStream zpos = new ZipOutputStream(os); 
        zpos.putNextEntry(new ZipEntry(fileName));
        GZIPOutputStream gos = (new GZIPOutputStream(zpos));
        fis.read(data);
        gos.write(data);

        zpos.flush();
        zpos.close();
        gos.flush();
        gos.close();
        dos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

我得到一个文件doc.pdf.zip.gz,但它有一个损坏的文件doc.pdf并且没有压缩,为什么?

最佳答案

我认为您可以为 zip 创建一个临时文件,将其添加到 gzip,然后删除创建的 zip。下面的代码示例应该可以帮助您。

public void ZIPandGZIP(String fileName, OutputStream os, String header) throws FileNotFoundException {
    byte[] buffer = new byte[1024];
    FileInputStream fis = null;
    FileOutputStream fos = null;
    ZipOutputStream zos = null;
    File tempZipFile = File.createTempFile(fileName, ".zip")
    try {
        fos = new FileOutputStream(tempZipFile);
        zos = new ZipOutputStream(fos);
        ZipEntry ze = new ZipEntry(fileName);
        zos.putNextEntry(ze);
        fis = new FileInputStream(fileName);

        int len;
        while ((len = fis.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }
        zos.closeEntry();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (fis != null) {
            fis.close();
        }
        if (zos != null) {
            zos.close();
        }
        if (fos != null) {
            fos.close();
        }
    }

    addGzipFileToStream(tempZipFile, os, header);
}

private void addGzipFileToStream(File zipFile, OutputStream os, String header) throws FileNotFoundException {

    byte[] buffer = new byte[1024];
    DataOutputStream dos = null;
    GZIPOutputStream gzos = null;
    FileInputStream inputStream = null;
    try {
        dos = new DataOutputStream(os);
        dos.writeBytes(header);

        gzos = new GZIPOutputStream(os);
        inputStream = new FileInputStream(zipFile);

        int len;
        while ((len = inputStream.read(buffer)) > 0) {
            gzos.write(buffer, 0, len);
        }
        gzos.finish();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (gzos != null) {
            gzos.close();
        }
        if (dos != null) {
            dos.close();
        }
        zipFile.delete();
    }
}

希望有帮助。

关于java - 如何在java中将.zip压缩为.gz?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36264105/

相关文章:

java - 如何检查 JSF 页面中是否存在 Activity session ?

java - 将用户输入限制为 HHMMSS

node.js - 我需要 Live 服务器上的 "node_modules"文件夹吗

javascript - 从服务器向客户端获取/获取数据时出现问题。 (客户端向服务器发送数据成功)

node.js - GraphQL/Prisma 客户端服务器错误 : Variable '$data' cannot be non input type 'LinkCreateInput!' .(第 1 行,第 18 列)

java - 解压文件夹而不覆盖新文件

java - 如何使用 Wicket 7 制作可缓存的图像?

javascript - 如何使用 javaScript 获取从 java 类传递到 native 脚本 jar 文件的变量数据

python - 如何使用一个列表中的条件从不同的列表中查找值

asp.net - 失败 - 在 Chrome 中获取 Zip 文件时出现网络错误