0

我有我想要写入保存在我的 Java 程序中的字符串中的文件的内容。我需要将它们写入.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));
    }   
}
4

1 回答 1

0

我编写了下面的示例代码,并使用 xc 作为我工作目录的输入。xcgz 是输出文件。我在 xcgz 上使用了 gunzip,它生成了一个名为 xc 的文件,所以它似乎工作正常。

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());
        }   
    }
}
于 2018-03-01T05:13:43.300 回答