java中ZLIB输入流的意外结束

发布于 2025-01-12 04:41:54 字数 1438 浏览 5 评论 0原文

public class GzExtractor implements Extractor {
    Logger logger = LoggerFactory.getLogger(GzExtractor.class);
    private static final int BUFFER_SIZE = 1024;

    byte[] buff = new byte[BUFFER_SIZE];
    private File file;
    private String destinationPath;

    public GzExtractor(File file, String destinationPath) {
        this.file = file;
        this.destinationPath = destinationPath;
    }

    public void extract() {

        try {
            File destDir = new File(destinationPath);
            if (!destDir.exists()) {
                destDir.mkdir();
            }
            GZIPInputStream gZipObj = new GZIPInputStream(new FileInputStream(file));
            String extractedFilename = file.getName().split(".gz")[0];
            OutputStream fosObj = new FileOutputStream(destinationPath + extractedFilename);
            int len;
            while ((len = gZipObj.read(buff)) > 0) {
                fosObj.write(buff, 0, len);
            }
            gZipObj.close();
            fosObj.close();
        } catch (Exception e) {
            logger.info("GZ Exception : {}",e.getMessage());
        }
    }
}

我收到意外 ZLIB 流的错误,但文件已成功提取。 我尝试了一些解决方案,但没有一个解决这个问题。我在阅读之前尝试关闭 gzip 流,因为我从这里的答案之一发现了这一点。但这当然会引发另一个错误。

我很困惑为什么我会得到这个,我想基本上消除错误语句。

[pool-1-thread-1] INFO service.ExtractorImpl.GzExtractor - GZ Exception : Unexpected end of ZLIB input stream
public class GzExtractor implements Extractor {
    Logger logger = LoggerFactory.getLogger(GzExtractor.class);
    private static final int BUFFER_SIZE = 1024;

    byte[] buff = new byte[BUFFER_SIZE];
    private File file;
    private String destinationPath;

    public GzExtractor(File file, String destinationPath) {
        this.file = file;
        this.destinationPath = destinationPath;
    }

    public void extract() {

        try {
            File destDir = new File(destinationPath);
            if (!destDir.exists()) {
                destDir.mkdir();
            }
            GZIPInputStream gZipObj = new GZIPInputStream(new FileInputStream(file));
            String extractedFilename = file.getName().split(".gz")[0];
            OutputStream fosObj = new FileOutputStream(destinationPath + extractedFilename);
            int len;
            while ((len = gZipObj.read(buff)) > 0) {
                fosObj.write(buff, 0, len);
            }
            gZipObj.close();
            fosObj.close();
        } catch (Exception e) {
            logger.info("GZ Exception : {}",e.getMessage());
        }
    }
}

I'm getting the error of unexpected ZLIB stream but the file is extracted successfully.
I tried some solutions but none of them solved this. I tried closing the gzip stream before reading as I found that from one of the answers here. But that throws another error of course.

I am confused why I'm getting this and I want to basically eliminate the error statement.

[pool-1-thread-1] INFO service.ExtractorImpl.GzExtractor - GZ Exception : Unexpected end of ZLIB input stream

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漆黑的白昼 2025-01-19 04:41:54

好吧,压缩文件的格式可能不正确。我使用的是 FTP 服务器,在其中上传不同类型的文件,即 zip、gzip、CSV 等。按照我的逻辑,解压缩将根据文件的压缩类型在文件上进行。从 FTP 服务器下载时,我忘记提及必须是二进制文件才能包含 zip 文件的文件类型。

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

因此,正在解压的文件格式一定不正确。也许这就是我收到此错误的原因。

将文件类型设置为此后,就可以正常工作了。

Okay so probably the compressed file was not in the correct format. I was using an FTP server where I was uploading different kinds of files i.e zip, gzip, CSV, etc. It was in my logic that the decompression would occur on the file according to the compression type of the file. While downloading from my FTP server, I forgot to mention the type of file that must be binary to include zip files.

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

So the file which was being decompressed must not be in a correct format. Maybe that is why I was getting this error.

After setting the file type to this, it worked okay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文