完美解决linux打包大于4G问题

源于下载数据量特别大,需要将下载文本打包到服务。jdk自带打包无法满足。使用如下方法能完美解决。

* 服务器打包*

/**
     * 使用apache工具打包完美解决大于4G问题
     */
    public void apacheZip() {
        String fgf = ConstKey.separator;
        logger.info("EAST--打包开始");
        try{
            String uploadFilePath = ConstKey.initSysMap.get(SysInit.offlinedown.name())+fgf+suitCode+fgf+fileNameDate+fgf;
            System.out.println("EAST打包路径:"+uploadFilePath);
            String zipPath = ConstKey.initSysMap.get(SysInit.offlinedown.name())+fgf+suitCode+fgf+fileNameDate+"_zip"+fgf;
            File file  = new File(zipPath);
            if(!file .exists()  && !file .isDirectory())
                 file .mkdirs();
            ZipUtils.compressFilesZip(uploadFilePath,zipPath+fileNameDate+".zip");
        }catch(Exception e){
            logger.error("EAST打包异常:"+e.getMessage());
            e.printStackTrace();
        }
        logger.info("EAST--打包结束");
    }

**打包方法**
 /**
     * 使用apache旗下的commons-compress
     * @param pathname
     * @param zipUrl
     * @return
     * @throws Exception
     */
    public static File compressFilesZip(String pathname, String zipUrl) throws Exception { 
        File srcFile = new File(pathname);
        File[] files = srcFile.listFiles();

        File zipFile = new File(zipUrl);
        if (!zipFile.exists()){   
            zipFile.createNewFile();   
        } 
        ZipArchiveOutputStream zaos = null;  
        try {  
            zaos = new ZipArchiveOutputStream(zipFile);  
            zaos.setUseZip64(Zip64Mode.AsNeeded);
            zaos.setEncoding("gbk"); // 解决BCSLinux环境 zip压缩包中的文件中文乱码 LYS
            //将每个文件用ZipArchiveEntry封装  
            //再用ZipArchiveOutputStream写到压缩文件中  
            for(File file : files) {  
                if(file != null) {  
                    ZipArchiveEntry zipArchiveEntry  = new ZipArchiveEntry(file,file.getName());  
                    zaos.putArchiveEntry(zipArchiveEntry);  
                    if(file.isDirectory()){  
                        continue;  
                    }  
                    InputStream is = null;  
                    try {  
                        is = new BufferedInputStream(new FileInputStream(file));  
                        byte[] buffer = new byte[1024 ];   
                        int len = -1;  
                        while((len = is.read(buffer)) != -1) {  
                            //把缓冲区的字节写入到ZipArchiveEntry  
                            zaos.write(buffer, 0, len);  
                        }  
                        zaos.closeArchiveEntry();   
                    }catch(Exception e) {  
                        throw new RuntimeException(e);  
                    }finally {  
                        if(is != null)  
                            is.close();  
                    }  
                }  
            }  
            zaos.finish();  
        }
        finally {  
            if(zaos != null) {  
                zaos.close();  
            }  
        }  
        return zipFile;
    }

参考:http://hw1287789687.iteye.com/blog/2050132

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值