论坛风格切换切换到宽版
  • 6422阅读
  • 0回复

[Java]java把多个文件打成zip压缩包 [复制链接]

上一主题 下一主题
离线路灯
 

发帖
562
金币
4350
威望
1778
股份
182
只看楼主 倒序阅读 使用道具 楼主  发表于: 2012-07-31
import java.io.*;
import org.apache.tools.zip.ZipOutputStream;
import org.apache.tools.zip.ZipEntry;
public class demo1 {
    public static void main( String[] args ) {
        try {
         String[] strs = new String[5];
         strs[0]="D:/workspace_myeclipse/GZRAIL/src/www/resource/word/lessonmanager/doc1/钟如燕.doc";
         strs[1]="D:/workspace_myeclipse/GZRAIL/src/www/resource/word/lessonmanager/doc1/马蓉.doc";
         strs[2]="D:/workspace_myeclipse/GZRAIL/src/www/resource/word/lessonmanager/doc1/邹青宜.doc";
         strs[3]="D:/workspace_myeclipse/GZRAIL/src/www/resource/word/lessonmanager/doc1/周黎.doc";
         strs[4]="D:/workspace_myeclipse/GZRAIL/src/www/resource/word/lessonmanager/doc1/邝莉莉.doc";
         //文件的列表 和 将要打成的zip文件的名称
            writeZip(strs,"123456");
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }
    private static void writeZip(String[] strs,String zipname) throws IOException {
        String[] files = strs;
        OutputStream os = new BufferedOutputStream( new FileOutputStream( "D:/workspace_myeclipse/GZRAIL/src/www/resource/word/lessonmanager/doc1/"+zipname+".zip" ) );
        ZipOutputStream zos = new ZipOutputStream( os );
        byte[] buf = new byte[8192];
        int len;
        for (int i=0;i<files.length;i++) {  
            File file = new File( files );
            if ( !file.isFile() ) continue;
            ZipEntry ze = new ZipEntry( file.getName() );
            zos.putNextEntry( ze );
            BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) );
            while ( ( len = bis.read( buf ) ) > 0 ) {
                zos.write( buf, 0, len );
            }
            zos.closeEntry();
        }
        zos.setEncoding("GBK");
        zos.closeEntry();
        zos.close();
        
        for(int i=0;i<files.length;i++){
         System.out.println("------------"+files );
         File file= new File(files );
         file.delete();
        }
    }
}
jar包下载 ant-1.7.1.jar.重命名.rar (1292 K) 下载次数:1




快速回复
限100 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
上一个 下一个