java 解压

    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;
    }
    

 

posted @ 2016-03-09 09:30  phyxis_xu  阅读(193)  评论(0编辑  收藏  举报