你可以在Java中如何追加到一个gzipped文件的末尾?

huangapple go评论112阅读模式
英文:

How do you append to the end of a gzipped file in Java?

问题

以下是您提供的代码的中文翻译:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPCompression {

    public static void main(String[] args) throws IOException {
        File file = new File("gziptest.zip");

        try (OutputStream os = new GZIPOutputStream(new FileOutputStream(file, true))) {
            os.write("test".getBytes());
        }

        try (GZIPInputStream inStream = new GZIPInputStream(new FileInputStream(file))) {
            while (inStream.available() > 0) {
                System.out.print((char) inStream.read());
            }
        }
    }
}

根据我所了解,根据您的描述,这段代码应该将 "test" 添加到 "gziptest.zip" 文件的末尾。但是,当运行代码时,文件没有被修改。奇怪的是,如果将 FileOutputStream(file, true) 更改为 FileOutputStream(file, false),则文件确实会被修改,但它的原始内容会被覆盖,这显然不是您想要的结果。

我使用的是JDK 14.0.1。

英文:

Here is what I've tried

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;


public class GZIPCompression {

    public static void main(String[] args) throws IOException {
        File file = new File("gziptest.zip");

        try ( OutputStream os = new GZIPOutputStream(new FileOutputStream(file, true))) {
            os.write("test".getBytes());
        }

        try ( GZIPInputStream inStream = new GZIPInputStream(new FileInputStream(file))) {
            while (inStream.available() > 0) {
                System.out.print((char) inStream.read());
            }
        }
    }
}

Based on what I've read, this should append "test" to the end of gziptest.zip, but when I run the code, the file doesn't get modified at all. The strange thing is that if I change FileOutputStream(file, true) to FileOutputStream(file, false), the file does get modified, but its original contents are overriden which is of course not what I want.

I am using JDK 14.0.1.

答案1

得分: 3

  1. Zip 和 GZip 是不同的。如果你要进行 GZip 测试,你的文件应该使用 .gz 扩展名,而不是 .zip。
  2. 要正确地将 "test" 附加到 GZip 数据的末尾,你应该首先使用 GZIPInputStream 从文件中读取,然后将 "test" 附加到未压缩的文本上,最后通过 GZipOutputStream 发送回去。
英文:

A couple of things here.

  1. Zip and GZip are different.. If you are doing a gzip test, your file should have the extension .gz, not .zip
  2. To properly append "test" to the end of the gzip data, you should first use a GZIPInputStream to read in from the file, then tack "test" onto the uncompressed text, and then send it back out through GZipOutputStream

huangapple
  • 本文由 发表于 2020年7月30日 04:08:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63161668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定