发布于 2016-07-05 06:56:59 | 162 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java设计模式,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


Java自动解压文件实例代码,需要的朋友可以参考一下


import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Enumeration; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipFile;


public class UnZipper {

    /**
     * 解压文件到当前目录 功能相当于右键 选择解压
     * @param zipFile
     * @param
     * @author gabriel
     */
    @SuppressWarnings("rawtypes")
    public static void unZipFiles(File zipFile)throws IOException{
        //得到压缩文件所在目录
        String path=zipFile.getAbsolutePath();
        path=path.substring(0,path.lastIndexOf("\\"));
       // System.out.println("path "+path);
        ZipFile zip = new ZipFile(zipFile);
        for(Enumeration entries =zip.entries();
                entries.hasMoreElements();){
            ZipEntry entry = (ZipEntry)entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = zip.getInputStream(entry);
            //outPath输出目录
            String outPath = (path+"\\"+zipEntryName).replaceAll("\\*", "/");;
            //System.out.println("outPath "+outPath);
            //判断路径是否存在,不存在则创建文件路径
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
            if(!file.exists()){
                file.mkdirs();
            }
            //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if(new File(outPath).isDirectory()){
                continue;
            }
            //输出文件路径信息
            System.out.println(outPath);

            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while((len=in.read(buf1))>0){
                out.write(buf1,0,len);
            }
            in.close();
            out.close();
            }
        System.out.println("******************解压完毕********************");
    }

   
    public static void main(String[] args) {
        try {
            unZipFiles(new File("D:\\all\\zip\\Default.adiumemoticonset.zip"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务