实现zip/tar的压缩与解压
java中实际是提供了对 zip等压缩格式的支持,但是为什么这里会用到ant呢?
原因主要有两个:
1. java提供的类对于包括有中文字符的路径,文件名支持不够好,你用其它第三方软件解压的时候就会存在乱码。而ant.jar就支持文件名或者路径包括中文字符。
2. ant.jar提供了强大的工具类,更加方便于我们对压缩与解压的操作。
注意事项:
1. 首先说明一下,关于皮肤或者类似于皮肤的Zip包,实际上公司可能会根据自己的规定或需求,自定义压缩包文件的结尾,实际上大多还是Zip包的格式. 具体部分的处理大致上是一样的,因此不再复述, 本文给出的例子已经有Zip包和Tar包的解压缩.
2. 还有要注意的是,此处为提升理解,因此加入zip/tar压缩,解压的界面,实际应用中此部分无需单独的界面展示(解压缩需要一定时间的话,则为加强用户体验,加入提示框与进度条),请自行编写解压缩管理类进行逻辑判断分别处理.
3. 测试时需要讲要解压缩的包导入sdcard目录下(若为其他目录,请修改代码中路径)
程序主界面及解压缩的界面:
接下来是解压缩核心的代码:
布局文件: antzip.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://www.easck.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dip"
android:layout_centerInParent="true">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioZip"
android:checked="true"
android:text="ZIP"/>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioTar"
android:text="TAR"
android:layout_marginLeft="10dip"/>
</RadioGroup>
<Button android:text="压缩" android:id="@+id/button1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingLeft="30dip" android:paddingRight="30dip"></Button>
<Button android:text="解压" android:id="@+id/button2"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingLeft="30dip" android:paddingRight="30dip"
android:layout_marginTop="20dip"></Button>
</LinearLayout>
</RelativeLayout>













