如何在Java中将.zip压缩为.gz? [英] How to compress a .zip to a .gz in java?

查看:153
本文介绍了如何在Java中将.zip压缩为.gz?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to compress a normal file to a .zip and then compress it as gzip file without creating a new file.

例如,假设我有一个pdf文档 doc.pdf ,我所要获得的是:doc.pdf.zip.gz

For example let's say that I have a pdf document doc.pdf, what I have to get is: doc.pdf.zip.gz

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

I don't want to creat a new file called doc.pdf.zip and then open it and gzip it.

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

I'm working with a server to get the file from the browser and return it back, this is my function:

 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已损坏,并且没有压缩原因?

I get a file doc.pdf.zip.gz but it has a corrupted file doc.pdf and not zipped why??

推荐答案

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

I think you can create a temporary file for the zip add it to gzip and then delete the zip that was created. The below code sample should help you with that.

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中将.zip压缩为.gz?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆