Java Compress Large File

纵饮孤独 提交于 2019-12-07 18:37:46

问题


I'm working on an app that works with some very large files each are around 180mb and there are 3 of them. I would like to add an option to my app to back these files up by compressing them in a zip or a tar or something. What is the best option would be to compress them down as much as possible in Java? Tar? Zip? Gzip?


回答1:


You can do this programmatically using Apache Compress.




回答2:


Look at the classes in the java.util.zip package.




回答3:


Alright went with zip here is the method i used. I found it online and modded it to junk the path and then just raised the buffer a little got about 450mbs of data down to 100mbs so not to bad :) thanks for the help

public void zipper(String[] filenames, String zipfile){
        byte[] buf = new byte[2048];
        try {
            String outFilename = zipfile;
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
            for (int i=0; i<filenames.length; i++) {
                FileInputStream in = new FileInputStream(filenames[i]);
                File file = new File(filenames[i]);
                out.putNextEntry(new ZipEntry(file.getName()));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
        } catch (IOException e) {
        }

    }

Plus 1 to both of you :)



来源:https://stackoverflow.com/questions/6542332/java-compress-large-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!