Android Zip檔案解壓縮代碼

來源:互聯網
上載者:User
/**    * 解壓縮功能.    * 將zipFile檔案解壓到folderPath目錄下.    * @throws Exception*/    public int upZipFile(File zipFile, String folderPath)throws ZipException,IOException {    //public static void upZipFile() throws Exception{        ZipFile zfile=new ZipFile(zipFile);        Enumeration zList=zfile.entries();        ZipEntry ze=null;        byte[] buf=new byte[1024];        while(zList.hasMoreElements()){            ze=(ZipEntry)zList.nextElement();                if(ze.isDirectory()){                Log.d("upZipFile", "ze.getName() = "+ze.getName());                String dirstr = folderPath + ze.getName();                //dirstr.trim();                dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");                Log.d("upZipFile", "str = "+dirstr);                File f=new File(dirstr);                f.mkdir();                continue;            }            Log.d("upZipFile", "ze.getName() = "+ze.getName());            OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));            InputStream is=new BufferedInputStream(zfile.getInputStream(ze));            int readLen=0;            while ((readLen=is.read(buf, 0, 1024))!=-1) {                os.write(buf, 0, readLen);            }            is.close();            os.close();            }        zfile.close();        Log.d("upZipFile", "finishssssssssssssssssssss");        return 0;    }    /**    * 給定根目錄,返回一個相對路徑所對應的實際檔案名稱.    * @param baseDir 指定根目錄    * @param absFileName 相對路徑名,來自於ZipEntry中的name    * @return java.io.File 實際的檔案*/    public static File getRealFileName(String baseDir, String absFileName){        String[] dirs=absFileName.split("/");        File ret=new File(baseDir);        String substr = null;        if(dirs.length>1){            for (int i = 0; i < dirs.length-1;i++) {                substr = dirs[i];                try {                    //substr.trim();                    substr = new String(substr.getBytes("8859_1"), "GB2312");                                    } catch (UnsupportedEncodingException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                ret=new File(ret, substr);                            }            Log.d("upZipFile", "1ret = "+ret);            if(!ret.exists())                ret.mkdirs();            substr = dirs[dirs.length-1];            try {                //substr.trim();                substr = new String(substr.getBytes("8859_1"), "GB2312");                Log.d("upZipFile", "substr = "+substr);            } catch (UnsupportedEncodingException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }                        ret=new File(ret, substr);            Log.d("upZipFile", "2ret = "+ret);            return ret;        }        return ret;    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.