Android中簡單使用Zip

初次使用Zip的壓縮和解壓,只是簡單使用

1、android中Zip的壓縮:

  • Zip的壓縮主要用到了ZipOutputStream和ZipEntry類
  • 小例子如下:
public String zipFileOptions(){
    //創(chuàng)建壓縮文件的路徑
    String zipFileName = Environment.getExternalStorageDirectory() 
    + "/" + UUID.randomUUID().toString().replace("-", "") + ".zip";
    //創(chuàng)建壓縮的文件對(duì)象
    File zipFile = new File(zipFileName);
    //創(chuàng)建InputStream對(duì)象
    InputStream is = null;
    //創(chuàng)建ZipOutputStream對(duì)象
    ZipOutputStream zos = null;
    try{
        //獲取ZipOutputStream對(duì)象實(shí)例
        zos = new ZipOutputStream(new FileOutputStream(zipFile));
        zos.setComment("hello");
    }catch(FileNotFoundException e){
        e.printStackTrace();
    }
    //FileBean中保存有需要壓縮的文件的文件路徑,mFileList中保存了所有需要被壓縮的文件路徑
    for(FileBean bean : mFileList){
        //根據(jù)路徑,創(chuàng)建需要被壓縮的文件對(duì)象,bean.getUrlPath()獲取到的是文件的路徑
        File file = new File(bean.getUrlPath());
        try{
            //獲取輸入流對(duì)象
            is = new FileInputStream(file);
            zos.setNextEntry(new ZipEntry(file.getName()));
            byte[] buffer = new byte[8*1024];
            int length = 0;
            while((length=is.read(buffer))!=-1){
                //將文件寫進(jìn)壓縮流
                zos.write(buffer,0,length);
            }
            is.close(); 
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    try{
        zos.close();
    }catch(Exception e){
        e.printStackTrace();
    }
    return zipFileName;
}

注意:文件的壓縮相對(duì)來說比較耗費(fèi)時(shí)間,因此,不能在主線程中進(jìn)行操作,需要另起線程進(jìn)行操作,以上例子用到的是AsyncTask。

2、Zip的下載:

  • 下載文件用到了java原始的API,在下載之前,首先需要獲得被下載文件的URL。
  • 然后自定義一個(gè)本地的文件路徑,用于裝載下載的Zip文件。
  • 然后在異步任務(wù)中進(jìn)行文件的下載。
  • 小例子如下:
/**
* 文件的下載方法
* @param url 文件的URL
* @param out 文件需要下載到的指定路徑目錄,需要自定義創(chuàng)建該路徑
**/
public File downloadZipFile(String url,String out){

    //定義URL對(duì)象
    URL mURL = null;
    //定義File對(duì)象
    File mFile = null;
    //定義URLConnection對(duì)象
    URLConnection urlConnection = null;
    int byteCopies = 0;
    //定義FileOutputStream對(duì)象
    FileOutputStream mOutputFileStream = null;

    try{
        //創(chuàng)建URL實(shí)例
        mURL = new URL(url);
        //獲取文件的名稱
        String fileName = new File(mURL.getFile()).getName();
        //根據(jù)指定文件目錄和文件名稱來創(chuàng)建文件
        mFile = new File(out,fileName);

        //獲取URLConnection對(duì)象
        urlConnection = mURL.openConnection();
        //獲取文件的長度
        int length = urlConnection.getContentLength();

        //如果文件已經(jīng)存在
        if (mFile.exists()&&length==mFile.length) {
            return mFile;
        }

        //獲取FileOutputStream對(duì)象
        mOutputFileStream = new FileOutputStream(mFile);
        //獲取InputStream對(duì)象
        InputStream is = urlConnection.getInputStream();

        //設(shè)置緩沖區(qū)的大小
        byte[] buffer = new byte[8*1024];
        //創(chuàng)建BufferedInputStream對(duì)象
        BufferedInputStream bis = new BufferedInputStream(is,8*1024);
        //創(chuàng)建BufferedOutputStream對(duì)象
        BufferedOutputStream bos = new BufferedOutputStream(mOutputFileStream,8*1024);

        int n = 0;

        while((n = bis.read(buffer,0,8*1024)) != -1){
            bos.write(buffer,0,n);
        }
        //清空緩沖區(qū)
        bos.flush();

        return mFile;

    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
            bos.close();
            bis.close();

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    return null;

}

Zip的解壓:

  • 首先需要獲取到下載到的壓縮文件
  • 完了以后通過異步任務(wù)對(duì)文件進(jìn)行壓縮
  • 下面的小例子通過解壓出來的文件名稱,再加上指定的文件目錄來保存解壓出來的文件
  • 小例子:
/**
* 進(jìn)行文件解壓的方法
* @param file 下載回來的壓縮文件
**/
public List<File> unZipFile(File file){

    //創(chuàng)建集合,保存解壓出來的文件
    List<File> mFileList = new ArrayList<>();
    //定義解壓出來的文件對(duì)象
    File outFile = null;

    try{
        //創(chuàng)建ZipFile對(duì)象
        ZipFile mZipFile = new ZipFile(file);
        //創(chuàng)建ZipInputStream對(duì)象
        ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
        //創(chuàng)建ZipEntry對(duì)象
        ZipEntry zipEntry = null;
        //定義InputStream對(duì)象
        InputStream is = null;
        //定義OutputStream對(duì)象
        OutputStream os = null;

        while((zipEntry=zis.getNextEntry())!=null){
            //拼湊路徑,創(chuàng)建解壓出來的文件對(duì)象
            outFile = new File(Environment.getExternalStorageDirectory() 
                + "/vaeh" + File.separator + zipEntry.getName());

            //判斷父級(jí)目錄是否存在
            if (!outFile.getParentFile().exists()) {
                //創(chuàng)建父級(jí)目錄
                outFile.getParentFile().mkdir();
            }

            //判斷文件是否存在
            if (!outFile.exists()) {
                //創(chuàng)建文件
                outFile.createNewFile();
            }

            //獲取is的實(shí)例
            is = mZipFile.getInputStream(zipEntry);
            os = new FileOutputStream(outFile);

            //創(chuàng)建緩沖區(qū)
            byte[] buffer = new byte[8*1024];
            int length = 0;

            while((length=is.read(buffer))!=-1){
                os.write(buffer,0,length);
            }
            //這里加多一次判斷是為了保險(xiǎn)起見,防止出現(xiàn)空指針
            if (outFile.exists()) {
                //將文件保存到集合中
                mFileList.add(outFile);
            }
            is.close();
            os.close();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return mFileList;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,728評(píng)論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評(píng)論 19 139
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,012評(píng)論 0 11
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,336評(píng)論 0 17
  • 這劇都完結(jié)大半年了,然后前兩天想起來要畫這對(duì),畫完再P回劇照,是不起很好玩呀,哈哈 原圖 鬼怪的小新娘 鬼怪先森 ...
    ipromiseido閱讀 1,995評(píng)論 17 34

友情鏈接更多精彩內(nèi)容