Android中簡單使用Zip

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

1、android中Zip的壓縮:

  • Zip的壓縮主要用到了ZipOutputStream和ZipEntry類
  • 小例子如下:
public String zipFileOptions(){
    //創建壓縮文件的路徑
    String zipFileName = Environment.getExternalStorageDirectory() 
    + "/" + UUID.randomUUID().toString().replace("-", "") + ".zip";
    //創建壓縮的文件對象
    File zipFile = new File(zipFileName);
    //創建InputStream對象
    InputStream is = null;
    //創建ZipOutputStream對象
    ZipOutputStream zos = null;
    try{
        //獲取ZipOutputStream對象實例
        zos = new ZipOutputStream(new FileOutputStream(zipFile));
        zos.setComment("hello");
    }catch(FileNotFoundException e){
        e.printStackTrace();
    }
    //FileBean中保存有需要壓縮的文件的文件路徑,mFileList中保存了所有需要被壓縮的文件路徑
    for(FileBean bean : mFileList){
        //根據路徑,創建需要被壓縮的文件對象,bean.getUrlPath()獲取到的是文件的路徑
        File file = new File(bean.getUrlPath());
        try{
            //獲取輸入流對象
            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){
                //將文件寫進壓縮流
                zos.write(buffer,0,length);
            }
            is.close(); 
        }catch(Exception e){
            e.printStackTrace();
        }
    }

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

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

2、Zip的下載:

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

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

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

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

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

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

        //設置緩沖區的大小
        byte[] buffer = new byte[8*1024];
        //創建BufferedInputStream對象
        BufferedInputStream bis = new BufferedInputStream(is,8*1024);
        //創建BufferedOutputStream對象
        BufferedOutputStream bos = new BufferedOutputStream(mOutputFileStream,8*1024);

        int n = 0;

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

        return mFile;

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

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

    return null;

}

Zip的解壓:

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

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

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

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

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

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

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

            //創建緩沖區
            byte[] buffer = new byte[8*1024];
            int length = 0;

            while((length=is.read(buffer))!=-1){
                os.write(buffer,0,length);
            }
            //這里加多一次判斷是為了保險起見,防止出現空指針
            if (outFile.exists()) {
                //將文件保存到集合中
                mFileList.add(outFile);
            }
            is.close();
            os.close();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return mFileList;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,739評論 18 399
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • 一. Java基礎部分.................................................
    wy_sure閱讀 3,832評論 0 11
  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,497評論 0 17
  • 這劇都完結大半年了,然后前兩天想起來要畫這對,畫完再P回劇照,是不起很好玩呀,哈哈 原圖 鬼怪的小新娘 鬼怪先森 ...
    ipromiseido閱讀 1,705評論 17 34