生成ZIP压缩包

jdk-1.4======================================
public String  genereateRARFile(String flow , List listFileName,String exname,String filepath)
       {
            String  file123= filepath + File.separator + "edfj" + flow + ".zip";
            String file123name= "edfj" + flow + ".zip";
         try
         {
              boolean flag=false;
                for (int i = 0; i < listFileName.size(); i++)
                  {
                   Map map=(Map)listFileName.get(i);
                    File file = new File(filepath+"/"+map.get("cct_ali_filename").toString());
                    if(file.exists()){
                         flag=true;
                    }
                  }
           if (listFileName.size() > 0)
           {
                if(flag){

                    File zipFileName = new File(file123);
                    InputStream input = null;
                    ZipOutputStream zipOut = null;
                    zipOut = new ZipOutputStream(new FileOutputStream(zipFileName));
                    for (int j = 0; j < listFileName.size(); j++)
                    {
                     Map map=(Map)listFileName.get(j);
                      File file = new File(filepath+"/"+map.get("cct_ali_filename").toString());
                      if(file.exists()){
                           EMPLog.log(EMPConstance.EMP_COMM, EMPLog.ERROR, 0,"读取文件名字:" +filepath+"/"+map.get("cct_ali_filename").toString());
                           input = new FileInputStream(file);
                           try {
                                zipOut.putNextEntry(new ZipEntry(file.getName()));
                           } catch (Exception e) {
                                continue;
                           }
                           int temp = 0;
                           while ((temp = input.read()) != -1) {
                             zipOut.write(temp);
                           }
                           input.close();
                      }
                    }
                  //把生成的excel文件压入ZIP包中
                          EMPLog.log(EMPConstance.EMP_COMM, EMPLog.ERROR, 0,"把生成的excel文件压入ZIP包中" +null);
                           File exfile = new File(filepath+"/"+exname);
                           if(exfile.exists()){
                                EMPLog.log(EMPConstance.EMP_COMM, EMPLog.ERROR, 0,"读取生成excel文件名字:" +filepath+"/"+exfile);
                                input = new FileInputStream(exfile);
                                     zipOut.putNextEntry(new ZipEntry(exfile.getName()));
                                int temp = 0;
                                while ((temp = input.read()) != -1) {
                                  zipOut.write(temp);
                                }
                                input.close();
                              EMPLog.log(EMPConstance.EMP_FLOW, EMPLog.DEBUG, 0, "删除excel文件",null);
                               exfile.delete();
                           }
                    zipOut.close();
           
                    EMPLog.log(EMPConstance.EMP_FLOW, EMPLog.DEBUG, 0, "额度申请附件下载zip包已经生成",null);
                 
                }else{
                     file123name="";
                }
           }
         }
         catch (Exception e)
         {
           e.printStackTrace();
             EMPLog.log(EMPConstance.EMP_FLOW, EMPLog.ERROR, 0, "额度申请附件下载zip包生成失败"+ e.getMessage());
         }
        
         return file123name;
       }
JDK-1.6=================解决中文名字乱码====================
两个jar  commons-compress-1.9.jar  commons-compress-1.9-javadoc.jar
 
package com.jn.test;ss


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.junit.Test;

import com.common.util.SystemUtil;
import com.io.hw.file.util.FileUtils;

public class ZIPTest {

        @Test
        public void test_01() {
                try {
                        FileOutputStream fou = new FileOutputStream("d:\\Temp\\a\\a\\b\\c.zip");
                        ArchiveOutputStream archOuts = new ArchiveStreamFactory()
                                        .createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);
                        if(archOuts instanceof ZipArchiveOutputStream){
                                ZipArchiveOutputStream zipOut=(ZipArchiveOutputStream)archOuts;
                                String file="D:\\Temp\\a\\password_密码.xls";
                                ZipArchiveEntry zipEntry=new ZipArchiveEntry(new File(file),SystemUtil.getFileSimpleName(file));
                                zipOut.putArchiveEntry(zipEntry);
                                zipOut.write(FileUtils.readBytes4file(file));
                                
                                zipOut.closeArchiveEntry();
                                zipOut.flush();
                                zipOut.finish();
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (ArchiveException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
}

(1)压缩多个文件:

@Test
        public void test_02() {
                try {
                        FileOutputStream fou = new FileOutputStream(
                                        "d:\\Temp\\a\\a\\b\\c.zip");
                        ArchiveOutputStream archOuts = new ArchiveStreamFactory()
                                        .createArchiveOutputStream(ArchiveStreamFactory.ZIP, fou);
                        if (archOuts instanceof ZipArchiveOutputStream) {
                                ZipArchiveOutputStream zipOut = (ZipArchiveOutputStream) archOuts;

                                String file01 = "D:\\Temp\\a\\password_密码.xls";
                                ZipArchiveEntry zipEntry = new ZipArchiveEntry(
                                                new File(file01), SystemUtil.getFileSimpleName(file01));
                                zipOut.putArchiveEntry(zipEntry);
                                zipOut.write(FileUtils.readBytes4file(file01));

                                String file02 = "D:\\Temp\\a\\ccc.jar";
                                ZipArchiveEntry zipEntry2 = new ZipArchiveEntry(
                                                new File(file01), SystemUtil.getFileSimpleName(file02));
                                zipOut.putArchiveEntry(zipEntry2);
                                zipOut.write(FileUtils.readBytes4file(file02));

                                zipOut.closeArchiveEntry();
                                zipOut.flush();
                                zipOut.finish();
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (ArchiveException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

猜你喜欢

转载自137015797.iteye.com/blog/2358672