通过GZipOutputStream

时间:2018-03-01 04:53:40

标签: java gzip gzipoutputstream

我想要写入我的Java程序中保存在Strings中的文件的内容。我需要将它们写入.txt文件,然后将它们压缩为.gz存档。我能够完成所有这些操作,但是当我从.gz存档中提取文件时,解压缩的文件不再具有.txt文件扩展名。

这是我的代码:

private void fillFileBody(OutputStream outputStream) throws IOException {
    Scanner scanner = new Scanner(new ByteArrayInputStream(this.fileBody.getBytes()));

    while(scanner.hasNextLine()){
        outputStream.write((scanner.nextLine() + "\n").getBytes());
    }

    scanner.close();
}

public void writeFileContentsToDisk(){
    GZIPOutputStream gZipOutStream = null;
    FileOutputStream initialTxtFileOutStream = null;
    FileInputStream initialTxtFileInStream = null;

    String txtFile = this.fileName + ".txt";

    try {
        initialTxtFileOutStream = new FileOutputStream(txtFile);
        initialTxtFileOutStream.write((this.fileHeader).getBytes());
        fillFileBody(initialTxtFileOutStream);
        initialTxtFileOutStream.write(this.fileTrailer.getBytes());

        initialTxtFileInStream = new FileInputStream(txtFile);
        gZipOutStream = new GZIPOutputStream(new FileOutputStream(this.fileName + ".gz"));

        byte[] buffer = new byte[1024];
        int length;
        while ((length = initialTxtFileInStream.read(buffer)) > 0) {
            gZipOutStream.write(buffer, 0, length);
        }
    } catch (Exception e) {
        LOGGER.error("Error writing file: " + Logger.getStackTrace(e));
    }

    try {
        gZipOutStream.close();
        initialTxtFileOutStream.close();
        initialTxtFileInStream.close();
    } catch (IOException e) {
        LOGGER.warn("Error closing i/o streams involved in writing file: " + Logger.getStackTrace(e));
    }   
}

1 个答案:

答案 0 :(得分:0)

我编写了下面的示例代码,并使用x.c作为我工作目录的输入。 x.c.gz是输出文件。我在x.c.gz上使用了gunzip,它生成了一个名为x.c的文件,所以看起来工作正常。

package compress;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import java.util.zip.GZIPOutputStream;
/**
 *
 * @author srikanthn
 */
public class Compress {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        compressFile(args[0]);
    }

    public static void compressFile(String file){
        GZIPOutputStream gZipOutStream = null;
        FileInputStream initialTxtFileInStream = null;

        String txtFile =  file;

        try {
            initialTxtFileInStream = new FileInputStream(txtFile);
            gZipOutStream = new GZIPOutputStream(new FileOutputStream(txtFile+ ".gz"));

            byte[] buffer = new byte[1024];
            int length;
            while ((length = initialTxtFileInStream.read(buffer)) > 0) {
                gZipOutStream.write(buffer, 0, length);
            }
        } catch (IOException e) {
            System.out.println("Error writing file: " + e.getStackTrace());
        }

        try {
            gZipOutStream.close();
            initialTxtFileInStream.close();
        } catch (IOException e) {
            System.out.println("Error closing i/o streams involved in writing file: " + e.getStackTrace());
        }   
    }
}