⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createzip.java

📁 制作zip文件常用的类
💻 JAVA
字号:
package com.util;
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.util.zip.*;
import sun.io.*;
public class CreateZip {
	
	private static CreateZip z=null;
	
	
	public static CreateZip getObj()
	{
		if(z==null)
			z=new CreateZip();
		return z;
	}
	public static ZipOutputStream zo=null;
	
	
	
	public static boolean deleteFile(String name){    
		String fileName = name;
		fileName=fileName.replaceAll("\\\\", "/");
	        File file = new File(fileName);    
	        if(file.isFile() && file.exists()){    
	            file.delete();    
	            //System.out.println("删除单个文件"+fileName+"成功!");    
	            return true;    
	        }else{    
	            //System.out.println("删除单个文件"+fileName+"失败!");    
	            return false;    
	        }    
	 }    
	
	public boolean createzip(String url,String name)
	{
		this.deleteFile(name);
		
		OutputStream os=null;
		BufferedOutputStream bs=null;
//System.out.println("====开始====");
		 try {
			 	String urls=url;
			 	String names=name;		 	
			 	urls=urls.replaceAll("\\\\", "/");
			 	names=names.replaceAll("\\\\", "/");
			 	//System.out.println("文件路径======"+urls);
			 	//System.out.println("文件名称======"+names);
	            os = new FileOutputStream(names);
	            bs = new BufferedOutputStream(os);
	            zo = new ZipOutputStream(bs);
	            //recursiveZip.zip("e:/recursive-zip/中文文件名.txt", new File("e:/recursive-zip"), zo, true, true);
	            zip(url, new File(url), zo, true, true);   
	           // System.out.println(3);

	           
	        } catch (Exception e) {
	        	
	        }
	        finally
	        {
	        	try {	 
	        		zo.closeEntry();
					zo.close();
					bs.close();
			        os.close();  
			        //System.out.println("关闭完成");
				} catch (IOException es) {
				}
	            
	        }//System.out.println("====完成====");
		return true;
	}
	
	public static void main(String[] args) {

		CreateZip recursiveZip = new CreateZip();
        //System.out.println("====开始====");
        try {
            //OutputStream os = new FileOutputStream("E:/work/wap2/WebRoot/contents/uu/doc-recursive.zip");
        	OutputStream os = new FileOutputStream("E:/work/wap2/WebRoot/contents/uu/"+recursiveZip.getTime()+".zip");
            BufferedOutputStream bs = new BufferedOutputStream(os);
            ZipOutputStream zo = new ZipOutputStream(bs);
            
            //recursiveZip.zip("e:/recursive-zip/中文文件名.txt", new File("e:/recursive-zip"), zo, true, true);
            recursiveZip.zip("E:/work/wap2/WebRoot/contents/uu", new File("E:/work/wap2/WebRoot/contents/uu"), zo, true, true);
            
            zo.closeEntry();
            zo.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
       // System.out.println("====完成====");
    }

    /**
     * @param path 要压缩的路径, 可以是目录, 也可以是文件.
     * @param basePath 如果path是目录,它一般为new File(path), 作用是:使输出的zip文件以此目录为根目录, 如果为null它只压缩文件, 不解压目录.
     * @param zo 压缩输出流
     * @param isRecursive 是否递归
     * @param isOutBlankDir 是否输出空目录, 要使输出空目录为true,同时baseFile不为null.
     * @throws IOException
     */
    public void zip(String path, File basePath, ZipOutputStream zo, boolean isRecursive, boolean isOutBlankDir) throws Exception {
        
        File inFile = new File(path);

        File[] files = new File[0];
        if(inFile.isDirectory()) {    //是目录
            files = inFile.listFiles();
        } else if(inFile.isFile()) {    //是文件
            files = new File[1];
            files[0] = inFile;
        }
        byte[] buf = new byte[1024];
        int len;
        //System.out.println("baseFile: "+baseFile.getPath());
        for(int i=0; i<files.length; i++) {
            String pathName = "";
            if(basePath != null) {
                if(basePath.isDirectory()) {
                    pathName = files[i].getPath().substring(basePath.getPath().length()+1);
                } else {//文件
                    pathName = files[i].getPath().substring(basePath.getParent().length()+1);
                }
            } else {
                pathName = files[i].getName();
            }
            //System.out.println(pathName);
            if(files[i].isDirectory()) {
                if(isOutBlankDir && basePath != null) {    
                    zo.putNextEntry(new ZipEntry(pathName+"/"));    //可以使空目录也放进去
                }
                if(isRecursive) {    //递归
                    zip(files[i].getPath(), basePath, zo, isRecursive, isOutBlankDir);
                   // System.out.println("深层路径列表"+files[i]);
                }
            } else {
                FileInputStream fin = new FileInputStream(files[i]);
                zo.putNextEntry(new ZipEntry(pathName));
                while((len=fin.read(buf))>0) {
                    zo.write(buf,0,len);
                }
                fin.close();         
            }
        }

        //zo.close();
        //zoo.close();
    }
    public String getTime()
	{
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE,-0);
		java.text.SimpleDateFormat fm=new java.text.SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
		//System.out.println(fm.format(cal.getTime()));
		return fm.format(cal.getTime());
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -