使用Java压缩ZIP中的大文件 [英] To Compress a big file in a ZIP with Java

查看:1904
本文介绍了使用Java压缩ZIP中的大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Java类ZipOutputStream压缩一个大文件(约450 MB).这个较大的尺寸导致我的JVM堆空间出现"OutOfMemory"错误的问题.发生这种情况是因为"zos.write(...)"方法将所有要压缩的文件内容存储在内部字节数组中,然后再压缩.

I have the need to compress a one Big file (~450 Mbyte) through the Java class ZipOutputStream. This big dimension causes a problem of "OutOfMemory" error of my JVM Heap Space. This happens because the "zos.write(...)" method stores ALL the file content to compress in an internal byte array before compressing it.

            origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(filePath);
        zos.putNextEntry(entry);

        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1)
        {
            zos.write(data, 0, count);
        }
        origin.close();

自然的解决方案是扩大JVM的堆内存空间,但是我想知道是否存在一种以流方式写入此数据的方法.我不需要很高的压缩率,因此也可以更改算法.

The natural solution will be to enlarge the heap memory space of the JVM, but I would like to know if there is a method to write this data in a streaming manner. I do not need an high compression rate so I could change the algorithm too.

有人对此有想法吗?

推荐答案

根据您对Sam的回复的评论,您显然已经创建了一个ZipOutputStream,其中包装了ByteArrayOutputStream. ByteArrayOutputStream当然会将压缩结果缓存在内存中.如果要将其写入磁盘,则必须将ZipOutputStream包裹在FileOutputStream周围.

According to your comment to Sam's response, you have obviously created a ZipOutputStream, which wraps a ByteArrayOutputStream. The ByteArrayOutputStream of course caches the compressed result in memory. If you want it written to disk, you have to wrap the ZipOutputStream around a FileOutputStream.

这篇关于使用Java压缩ZIP中的大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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