码迷,mamicode.com
首页 > 编程语言 > 详细

java 解压

时间:2016-03-09 10:59:03      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

    public static void parseZIP(String path,String filename,String resourcePath){
        System.out.println("filename:"+filename);
        String filename0 = filename.substring(0, filename.lastIndexOf("."));
        createFile(filename0);
        String pathDir = createDirs(filename0,resourcePath);
        
        try {
            // destination folder to extract the contents
            byte[] buf = new byte[1024];
            ZipInputStream zipinputstream = null;
            ZipEntry zipentry;
            zipinputstream = new ZipInputStream(new FileInputStream(path));
            zipentry = zipinputstream.getNextEntry();
            while (zipentry != null) {
 
                // for each entry to be extracted
                String entryName = zipentry.getName();

//                System.out.println(entryName);
 
                int n;
                FileOutputStream fileoutputstream;
//                File newFile = new File(entryName);
 
                if (!zipentry.isDirectory()) {
//                    System.out.println("File to be extracted....." + entryName);
                    fileoutputstream = new FileOutputStream(pathDir+File.separator+entryName);
                    while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
                        fileoutputstream.write(buf, 0, n);
                    }
                    fileoutputstream.close();
                }
 
                zipinputstream.closeEntry();
                zipentry = zipinputstream.getNextEntry();
            }
            zipinputstream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    //解析 Rar文件  放到指定的目录 环境
    public static void parseRAR(String path,String filename,String resourcePath) {
        
        if(!path.toLowerCase().endsWith(".rar")){
            
        }
        
        String filename0 = filename.substring(0, filename.indexOf(".rar"));
        createFile(filename0);
        String pathDir = createDirs(filename0,resourcePath);
        FileOutputStream  out = null; 
        Archive a = null;

        try {
            a = new Archive(new File(path));
            
            FileHeader fh = a.nextFileHeader();
            
            while(fh != null){

                String name = fh.getFileNameString();
                out = new FileOutputStream(new File(pathDir+File.separator+name));
                a.extractFile(fh, out);
                fh = a.nextFileHeader();
                out.close();
            }

            a.close();
        } catch (RarException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        
        //System.out.println("--------------end--------------");
    }
    
    //解析Rar文件 得到里面配置文件 并得到配置文件名称 :包名
    private static  String  getPackageName(String path){
    
        if(!path.toLowerCase().endsWith(".rar")){
            
        }
        BufferedOutputStream  out = null; 
        String retValue = "";
        Archive a = null;
        try {
            a = new Archive(new File(path));
            FileHeader fh = a.nextFileHeader();
            while(fh != null){
                if(!fh.isDirectory()&&fh.getFileNameString().endsWith(".txt")){
                    
                    retValue = fh.getFileNameString();
                    retValue = retValue.substring(retValue.indexOf(File.separator)+1,retValue.lastIndexOf(".txt"));
                    out = new BufferedOutputStream(new FileOutputStream(new File(ParseConfig.getValue("configFiles")+File.separator+retValue+".txt")));
                    a.extractFile(fh, out);
                    out.flush();
                    out.close();
                    break;
                }
                fh = a.nextFileHeader();
            }

            a.close();
        } catch (RarException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return retValue;
    }
    

 

java 解压

标签:

原文地址:http://www.cnblogs.com/phyxis/p/5256838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!