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用户有读写权限,如果其他用户需要访问,需要改变权限。

Caused by: MobileHarnessException: Failed to unzip file /root/xts/mcts_dynamic_download/android/xts/mcts/35/arm64/android-mcts-permission.zip to dir /tmp/olc_server_temp_files/session_6f0a63ff-ea2c-4f5b-9df6-49f000b42fd4/job_tmp_xts-tradefed-test-job_db105dd1-4158-428b-a2e2-7cb7541c73d9/test_5394de8f-68c8-4c07-86cb-9cd56e048be1/android/xts/mcts [MH|UNDETERMINED|LOCAL_FILE_UNZIP_ERROR|30123] [MobileHarnessException],Caused by: com.google.devtools.mobileharness.shared.util.command.CommandFailureException: Failed command with exit_code=9 and success_exit_codes=[0], result=[code=9, out=[ End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one disk of a multi-part archive. In the latter case the central directory and zipfile comment will be found on the last disk(s) of this archive. note: /root/xts/mcts_dynamic_download/android/xts/mcts/35/arm64/android-mcts-permission.zip may be a plain executable, not an archive unzip: cannot find zipfile directory in one of /root/xts/mcts_dynamic_download/android/xts/mcts/35/arm64/android-mcts-permission.zip or /root/xts/mcts_dynamic_download/android/xts/mcts/35/arm64/android-mcts-permission.zip.zip, and cannot find /root/xts/mcts_dynamic_download/android/xts/mcts/35/arm64/android-mcts-permission.zip.ZIP, period. Archive: /root/xts/mcts_dynamic_download/android/xts/mcts/35/arm64/android-mcts-permission.zip ], err=[]], command=[unzip -o /root/xts/mcts_dynamic_download/android/xts/mcts/35/arm64/android-mcts-permission.zip] [MH|UNDETERMINED|COMMAND_EXEC_FAIL|39998] [CommandFailureException]
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值