标签:
上一篇中提到对于Zip包的解压和压缩需要借助Ant 实现,我经过参考了其他的资料,整理后并加上了一些自己的看法:
这里就具体地讲下如何使用Ant进行解压缩及其原因:
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://schemas.android.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>
AntZipActivity:
- public class AntZipActivity extends Activity {
- public static final String TYPE = "type";
- public static final int TYPE_ZIP = -1;
- public static final int TYPE_TAR = 1;
-
- public static final String SUFFIX_ZIP = ".zip";
- public static final String SUFFIX_TAR = ".tar";
-
- private Button btnDoCompress;
- private Button btnDecompress;
-
- private RadioButton radioZip;
- private RadioButton radioTar;
-
- private boolean isZip = true;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.antzip);
- radioZip = (RadioButton)findViewById(R.id.radioZip);
- isZip = true;
- radioZip.setChecked(true);
- radioZip.setOnCheckedChangeListener(new OnCheckedChangeListener() {
-
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- System.out.println("radioZip:"+isChecked);
- if(isChecked)
- {
- isZip = true;
- }
- }
- });
- radioTar = (RadioButton)findViewById(R.id.radioTar);
- radioTar.setOnCheckedChangeListener(new OnCheckedChangeListener() {
-
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- System.out.println("radioTar:"+isChecked);
- if(isChecked)
- {
- isZip = false;
- }
- }
- });
- btnDoCompress = (Button)findViewById(R.id.button1);
- btnDoCompress.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- Intent i = new Intent(AntZipActivity.this,DozipActivity.class);
- i.putExtra(TYPE, isZip?TYPE_ZIP:TYPE_TAR);
- AntZipActivity.this.startActivity(i);
- }
- });
- btnDecompress = (Button)findViewById(R.id.button2);
- btnDecompress.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- Intent i = new Intent(AntZipActivity.this,UnzipActivity.class);
- i.putExtra(TYPE, isZip?TYPE_ZIP:TYPE_TAR);
- AntZipActivity.this.startActivity(i);
- }
- });
- }
- }
DozipActivity:
- public class DozipActivity extends Activity implements OnClickListener{
- private EditText etPath;
- private EditText etDest;
- private Button btnDozip;
- private TextView tvTip;
-
- private String srcPath;
- private String zipDest;
-
- private int type;
- private String suffix;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setTitle("Ant-压缩");
- type = getIntent().getIntExtra(AntZipActivity.TYPE, AntZipActivity.TYPE_ZIP);
- suffix = type==AntZipActivity.TYPE_ZIP ? AntZipActivity.SUFFIX_ZIP:AntZipActivity.SUFFIX_TAR;
- setContentView(R.layout.dozip);
-
- etPath = (EditText)findViewById(R.id.editText1);
- etDest = (EditText)findViewById(R.id.editText2);
-
- etPath.setText("/sdcard/antzip");
- etDest.setText("/sdcard/antzip"+suffix);
- btnDozip = (Button)findViewById(R.id.button);
- tvTip = (TextView)findViewById(R.id.tv_tip);
- btnDozip.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- srcPath = etPath.getEditableText().toString();
- if(TextUtils.isEmpty(srcPath))
- {
- Toast.makeText(this, "请指定一个路径", Toast.LENGTH_SHORT).show();
- return;
- }
- File srcFile = new File(srcPath);
- if(!srcFile.exists())
- {
- Toast.makeText(this, "指定的压缩包不存在", Toast.LENGTH_SHORT).show();
- return;
- }
- zipDest = etDest.getEditableText().toString();
- if(TextUtils.isEmpty(zipDest))
- {
-
- zipDest = srcFile.getParent();
- }
- System.out.println("zip name:"+zipDest);
-
- if(zipDest.endsWith(File.separator))
- {
- zipDest+=srcFile.getName()+suffix;
- }
- else
- {
-
- if(!zipDest.endsWith(suffix))
- {
- zipDest +=suffix;
- }
- }
-
- if(type == AntZipActivity.TYPE_ZIP)
- {
-
- ZipUtil zipp = new ZipUtil();
- zipp.doZip(srcPath, zipDest);
- }
-
- else
- {
- TarUtil tarr = new TarUtil();
- tarr.doTar(srcPath, zipDest);
- }
-
- tvTip.setText("压缩文件路径:"+zipDest);
- Toast.makeText(this, "压缩完成", Toast.LENGTH_SHORT).show();
- }
- }
解压缩工具类ZipUtil:
- public class ZipUtil {
- private ZipFile zipFile;
- private ZipOutputStream zipOut;
- private int bufSize;
- private byte[] buf;
-
- public ZipUtil(){
-
- this.bufSize = 1024*4;
- this.buf = new byte[this.bufSize];
- }
-
-
- public void doZip(String srcFile, String destFile) {
- File zipFile = new File(srcFile);
- try {
-
- this.zipOut = new ZipOutputStream(new BufferedOutputStream(
- new FileOutputStream(destFile)));
-
- zipOut.setComment("comment");
-
- zipOut.setEncoding("GBK");
-
- zipOut.setMethod(ZipOutputStream.DEFLATED);
-
-
- zipOut.setLevel(Deflater.BEST_COMPRESSION);
-
- handleFile(zipFile, this.zipOut,"");
-
- this.zipOut.close();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
-
-
- private void handleFile(File zipFile, ZipOutputStream zipOut,String dirName) throws IOException {
- System.out.println("遍历文件:"+zipFile.getName());
-
- if(zipFile.isDirectory())
- {
- File[] files = zipFile.listFiles();
-
- if (files.length == 0) {
-
- this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()+File.separator));
- this.zipOut.closeEntry();
- } else {
- for (File file : files) {
-
- handleFile(file, zipOut, dirName+zipFile.getName()+File.separator);
- }
- }
- }
-
- else
- {
- FileInputStream fileIn = new FileInputStream(zipFile);
-
- this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()));
- int length = 0;
-
- while ((length = fileIn.read(this.buf)) > 0) {
- this.zipOut.write(this.buf, 0, length);
- }
-
- this.zipOut.closeEntry();
- }
-
- }
-
-
- public void unZip(String unZipfile, String destFile) {
- FileOutputStream fileOut;
- File file;
- InputStream inputStream;
-
- try {
-
- this.zipFile = new ZipFile(unZipfile);
-
- for (@SuppressWarnings("unchecked")
- Enumeration<ZipEntry> entries = this.zipFile.getEntries(); entries
- .hasMoreElements();) {
- ZipEntry entry = entries.nextElement();
-
- file = new File(destFile+File.separator+entry.getName());
-
- if (entry.isDirectory()) {
- file.mkdirs();
- } else {
-
- File parent = file.getParentFile();
- if (!parent.exists()) {
- parent.mkdirs();
- }
-
- inputStream = zipFile.getInputStream(entry);
-
- fileOut = new FileOutputStream(file);
- int length = 0;
-
- while ((length = inputStream.read(this.buf)) > 0) {
- fileOut.write(this.buf, 0, length);
- }
- fileOut.close();
- inputStream.close();
- }
- }
- this.zipFile.close();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
Android 打造自己的个性化应用(五):仿墨迹天气实现续--> 使用Ant实现zip/tar的压缩与解压
标签:
原文地址:http://www.cnblogs.com/dongweiq/p/4249935.html