android to unzip zip files

最近在android中做了一個 解壓zip的功能。本來想用Runtime執行shell命令來做。

但是有的busybox版本問題,unzip功能太簡單,不能指定工作目錄,解壓到當前目錄又沒有權限。只能想別的辦法,原來java自帶有zip的解壓縮庫。

 

轉自http://www.funtee.net/archives/27

 

there is two way to unzip zip files for android;

在Android平臺中如何實現Zip文件的解壓縮功能呢? 因爲Android內部已經集成了zlib庫,對於英文和非密碼的Zip文件解壓縮還是比較簡單的,可以在Android上任何版本中使用,Unzip這個靜態方法比較簡單,參數一爲源zip文件的完整路徑,參數二爲解壓縮後存放的文件夾。

first way: unzip(String filename,String targetDir);
private static void Unzip(String zipFile, String targetDir)
{
    int BUFFER = 4096; //這裏緩衝區我們使用4KB,
    String strEntry; //保存每個zip的條目名稱
    try
    {
        BufferedOutputStream dest = null; //緩衝輸出流
        FileInputStream fis = new FileInputStream(zipFile);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry; //每個zip條目的實例
       
        while ((entry = zis.getNextEntry()) != null)
        {
       
            try
            {
                Log.i(“Unzip: “,”=”+ entry);
                int count;
                byte data[] = new byte[BUFFER];
                strEntry = entry.getName();
               
                File entryFile = new File(targetDir + strEntry);
                File entryDir = new File(entryFile.getParent());
                if (!entryDir.exists())
                {
                    entryDir.mkdirs();
                }
               
                FileOutputStream fos = new FileOutputStream(entryFile);
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zis.read(data, 0, BUFFER)) != -1)
                {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        zis.close();
    }
    catch (Exception cwj)
    {
     cwj.printStackTrace();
    }
}

the second way : unzip(InputStream zipFileName, String outputDirectory));

//定義assetmanager對象
AssetManager assetManager = getAssets();
// 需要解壓的對象
InputStream dataSource = assetManager.open(“ShiningTrip.zip”);
//    調用解壓的方法
ZipUtil.unzip(dataSource, android.os.Environment
.getExternalStorageDirectory()  + “”);

public static void unzip(InputStream zipFileName, String outputDirectory)
{
    try
     {
        ZipInputStream in = new ZipInputStream(zipFileName);
        // 獲取ZipInputStream中的ZipEntry條目,一個zip文件中可能包含多個ZipEntry,
        // 當getNextEntry方法的返回值爲null,則代表ZipInputStream中沒有下一個ZipEntry,
        // 輸入流讀取完成;
        ZipEntry entry = in.getNextEntry();
        while (entry != null)
        {
       
            // 創建以zip包文件名爲目錄名的根目錄
            File file = new File(outputDirectory);
            file.mkdir();
            if (entry.isDirectory())
            {
                String name = entry.getName();
                name = name.substring(0, name.length() – 1);
               
                file = new File(outputDirectory + File.separator + name);
                file.mkdir();
           
            }
            else
            {
                file = new File(outputDirectory + File.separator + entry.getName());
                file.createNewFile();
                FileOutputStream out = new FileOutputStream(file);
                int b;
                while ((b = in.read()) != -1)
                {
                    out.write(b);
                }
                out.close();
            }
            // 讀取下一個ZipEntry
            entry = in.getNextEntry();
        }
        in.close();
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        // TODO 自動生成 catch 塊
        e.printStackTrace();
    }
}

 

經過測試單個文件解壓正確,後續測試多文件多目錄等。注意解壓後只有root用戶有讀寫權限,如果其他用戶需要訪問,需要改變權限。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章