下面的小代码为周未时间所写。主要利用了ant.jar对文件进行压缩处理。以下为全部代码,界面是随便所画。如有什么问题还希望各位指出。
xml文件代码 :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/backup" android:text="backup" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/recover" android:text="recover"/> </LinearLayout>
android 代码:
package com.android.file; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.zip.ZipException; import com.android.file.zip.ZipControl; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class FileAndroidActivity extends Activity implements OnClickListener { private final static String TAG = "FileAndroidActivity"; private String[] fileSrcStrings;//指定压缩源,可以是目录或文件的数组 private String decompressDirString="";//解压路径 private String archiveString="";//压缩包路径 private String commentString="Androi Java Zip 测试.";//压缩包注释 private ZipControl mZipControl; private Button mBackupButton; private Button mRecoverButton; private String srcString;//第一个文件的路径 private String srcTwoString;//第二个文件的路径 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBackupButton = (Button) findViewById(R.id.backup); mRecoverButton = (Button) findViewById(R.id.recover); mBackupButton.setOnClickListener(this); mRecoverButton.setOnClickListener(this); String pathString =this.getCacheDir().toString(); Log.e(TAG, "path is "+ pathString); archiveString=pathString+"/zip"; decompressDirString = pathString+"/unzip"; File zipFile = new File(archiveString); if(!zipFile.exists()) { zipFile.mkdir(); Log.e(TAG, "make zipdir success"); } else { Log.e(TAG, "exit zipdir"); } File unZipFile = new File(decompressDirString); if(!unZipFile.exists()) { unZipFile.mkdir(); Log.e(TAG, "make undir success"); } else { Log.e(TAG, "exit undir"); } String strFileOne = pathString+"/fileoneDir"; File fileOneFile = new File(strFileOne); if(!fileOneFile.exists()) { fileOneFile.mkdir(); Log.e(TAG, "make fileonedir success"); } else { Log.e(TAG, "exit fileonedir"); } srcString = strFileOne+"/testfile01.txt"; String strFileTwo =pathString+"/filetwoDir"; File fileOneFileTwo = new File(strFileTwo); if(!fileOneFileTwo.exists()) { fileOneFileTwo.mkdir(); Log.e(TAG, "make filetwoDir success"); } else { Log.e(TAG, "exit filetwoDir"); } srcTwoString = strFileTwo+"/testfile02.txt"; fileSrcStrings= new String[]{srcString,srcTwoString}; mZipControl = new ZipControl(); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.backup: Log.e(TAG, "start backup"); try { mZipControl.writeByApacheZipOutputStream(fileSrcStrings, archiveString+"/test.zip", commentString); } catch (FileNotFoundException e) { Log.e(TAG, e.toString()); // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { Log.e(TAG, e.toString()); // TODO Auto-generated catch block e.printStackTrace(); } break; case R.id.recover: try { ZipControl.readByApacheZipFile(archiveString+"/test.zip", decompressDirString); } catch (FileNotFoundException e) { Log.e(TAG, e.toString()); // TODO Auto-generated catch block e.printStackTrace(); } catch (ZipException e) { Log.e(TAG, e.toString()); // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.e(TAG, "start recover "); break; default: break; } } }
对文件压缩的主要代码类:
package com.android.file.zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; import java.util.zip.CheckedOutputStream; import java.util.zip.Deflater; import java.util.zip.ZipException; import java.util.zip.ZipInputStream; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; import android.util.Log; /** * * [一句话功能简述]<BR> * [功能详细描述] * @author zhouxin * @version [Android MTVClient C01, 2011-3-4] */ public class ZipControl { private static boolean isCreateSrcDir = false;//是否创建源目录 在这里的话需要说明下。如果需要创建源目录的话。就在这里设为true否则为false; private static String TAG="ZipControl"; /** * * [对指定路径下文件的压缩处理]<BR> * [功能详细描述] * * @param src 径地址 * @param archive 指定到压缩文件夹的路径 * @param comment 描述 * @throws FileNotFoundException 文件没有找到异常 * @throws IOException IO输入异常 */ public void writeByApacheZipOutputStream(String[] src, String archive, String comment) throws FileNotFoundException, IOException { Log.e(TAG, "writeByApacheZipOutputStream"); //----压缩文件: FileOutputStream f = new FileOutputStream(archive); //使用指定校验和创建输出流 CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32()); ZipOutputStream zos = new ZipOutputStream(csum); //支持中文 zos.setEncoding("GBK"); BufferedOutputStream out = new BufferedOutputStream(zos); //设置压缩包注释 zos.setComment(comment); //启用压缩 zos.setMethod(ZipOutputStream.DEFLATED); //压缩级别为最强压缩,但时间要花得多一点 zos.setLevel(Deflater.BEST_COMPRESSION); // 如果为单个文件的压缩在这里修改 for (int i = 0; i < src.length; i++) { Log.e(TAG, "src["+i+"] is "+src[i]); File srcFile = new File(src[i]); if (!srcFile.exists() || (srcFile.isDirectory() && srcFile.list().length == 0)) { Log.e(TAG, "!srcFile.exists()"); throw new FileNotFoundException( "File must exist and ZIP file must have at least one entry."); } String strSrcString = src[i]; //获取压缩源所在父目录 strSrcString = strSrcString.replaceAll("////", "/"); String prefixDir = null; if (srcFile.isFile()) { prefixDir = strSrcString.substring(0, strSrcString .lastIndexOf("/") + 1); } else { prefixDir = (strSrcString.replaceAll("/$", "") + "/"); } //如果不是根目录 if (prefixDir.indexOf("/") != (prefixDir.length() - 1) && isCreateSrcDir) { prefixDir = prefixDir.replaceAll("[^/]+/$", ""); } //开始压缩 writeRecursive(zos, out, srcFile, prefixDir); } out.close(); // 注:校验和要在流关闭后才准备,一定要放在流被关闭后使用 Log.e(TAG, "Checksum: " + csum.getChecksum().getValue()); @SuppressWarnings("unused") BufferedInputStream bi; } /** * * [* 使用 org.apache.tools.zip.ZipFile 解压文件,它与 java 类库中的 * java.util.zip.ZipFile 使用方式是一新的,只不过多了设置编码方式的 接口。 * * 注,apache 没有提供 ZipInputStream 类,所以只能使用它提供的ZipFile 来读取压缩文件。]<BR> * * @param archive 压缩包路径 * @param decompressDir 解压路径 * @throws IOException * @throws FileNotFoundException * @throws ZipException */ @SuppressWarnings("unchecked") public static void readByApacheZipFile(String archive, String decompressDir) throws IOException, FileNotFoundException, ZipException { Log.e(TAG, "readByApacheZipFile"); BufferedInputStream bi; ZipFile zf = new ZipFile(archive, "GBK");//支持中文 Enumeration e = zf.getEntries(); while (e.hasMoreElements()) { ZipEntry ze2 = (ZipEntry) e.nextElement(); String entryName = ze2.getName(); String path = decompressDir + "/" + entryName; if (ze2.isDirectory()) { Log.e(TAG, "正在创建解压目录 - " + entryName); File decompressDirFile = new File(path); if (!decompressDirFile.exists()) { decompressDirFile.mkdirs(); } } else { Log.e(TAG, "正在创建解压文件 - " + entryName); String fileDir = path.substring(0, path.lastIndexOf("/")); File fileDirFile = new File(fileDir); if (!fileDirFile.exists()) { fileDirFile.mkdirs(); } BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(decompressDir + "/" + entryName)); bi = new BufferedInputStream(zf.getInputStream(ze2)); byte[] readContent = new byte[1024]; int readCount = bi.read(readContent); while (readCount != -1) { bos.write(readContent, 0, readCount); readCount = bi.read(readContent); } bos.close(); } } zf.close(); } /** * * [使用 java api 中的 ZipInputStream 类解压文件,但如果压缩时采用了 * org.apache.tools.zip.ZipOutputStream时,而不是 java 类库中的 * java.util.zip.ZipOutputStream时,该方法不能使用,原因就是编码方 式不一致导致,运行时会抛如下异常: * java.lang.IllegalArgumentException at * java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:290) * * 当然,如果压缩包使用的是java类库的java.util.zip.ZipOutputStream 压缩而成是不会有问题的,但它不支持中文 ]<BR> * [功能详细描述] * * @param archive 压缩包路径 * @param decompressDir 解压路径 * @throws FileNotFoundException * @throws IOException */ public static void readByZipInputStream(String archive, String decompressDir) throws FileNotFoundException, IOException { BufferedInputStream bi; //----解压文件(ZIP文件的解压缩实质上就是从输入流中读取数据): Log.e(TAG, "开始读压缩文件"); FileInputStream fi = new FileInputStream(archive); CheckedInputStream csumi = new CheckedInputStream(fi, new CRC32()); ZipInputStream in2 = new ZipInputStream(csumi); bi = new BufferedInputStream(in2); java.util.zip.ZipEntry ze;//压缩文件条目 //遍历压缩包中的文件条目 while ((ze = in2.getNextEntry()) != null) { String entryName = ze.getName(); if (ze.isDirectory()) { Log.e(TAG,"正在创建解压目录 - " + entryName); File decompressDirFile = new File(decompressDir + "/" + entryName); if (!decompressDirFile.exists()) { decompressDirFile.mkdirs(); } } else { Log.e(TAG, "正在创建解压文件 - " + entryName); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(decompressDir + "/" + entryName.substring(entryName.lastIndexOf("//"), entryName.length() - (entryName.lastIndexOf("//") - 2)))); byte[] buffer = new byte[1024]; int readCount = bi.read(buffer); while (readCount != -1) { bos.write(buffer, 0, readCount); readCount = bi.read(buffer); } bos.close(); } } bi.close(); Log.e(TAG, "Checksum: " + csumi.getChecksum().getValue()); } /** * * [递归压缩 * * 使用 org.apache.tools.zip.ZipOutputStream 类进行压缩,它的好处就是支持中文路径, 而Java类库中的 * java.util.zip.ZipOutputStream 压缩中文文件名时压缩包会出现乱码。 使用 apache 中的这个类与 java * 类库中的用法是一新的,只是能设置编码方式了。]<BR> * [功能详细描述] * * @param zos * @param bo * @param srcFile * @param prefixDir * @throws IOException * @throws FileNotFoundException */ private static void writeRecursive(ZipOutputStream zos, BufferedOutputStream bo, File srcFile, String prefixDir) throws IOException, FileNotFoundException { Log.e(TAG, "writeRecursive"); ZipEntry zipEntry; String filePath = srcFile.getAbsolutePath().replaceAll("////", "/") .replaceAll("//", "/"); if (srcFile.isDirectory()) { filePath = filePath.replaceAll("/$", "") + "/"; } String entryName = filePath.replace(prefixDir, "").replaceAll("/$", ""); if (srcFile.isDirectory()) { if (!"".equals(entryName)) { Log.e(TAG, "正在创建目录 - " + srcFile.getAbsolutePath() + " entryName=" + entryName); //如果是目录,则需要在写目录后面加上 / zipEntry = new ZipEntry(entryName + "/"); zos.putNextEntry(zipEntry); } File srcFiles[] = srcFile.listFiles(); for (int i = 0; i < srcFiles.length; i++) { writeRecursive(zos, bo, srcFiles[i], prefixDir); } } else { Log.e(TAG,"正在写文件 - " + srcFile.getAbsolutePath() + " entryName=" + entryName ); BufferedInputStream bi = new BufferedInputStream( new FileInputStream(srcFile)); //开始写入新的ZIP文件条目并将流定位到条目数据的开始处 zipEntry = new ZipEntry(entryName); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int readCount = bi.read(buffer); while (readCount != -1) { bo.write(buffer, 0, readCount); readCount = bi.read(buffer); } //注,在使用缓冲流写压缩文件时,一个条件完后一定要刷新一把,不 //然可能有的内容就会存入到后面条目中去了 bo.flush(); //文件读完后关闭 bi.close(); } } }
您还没有登录,请您登录后再发表评论
Android端zip压缩与解压,目前暂时只做zip格式支持,基于Zip4j 进行扩展成工具类,支持对单个文件,多个文件以及文件夹进行压缩,对压缩文件解压到到指定目录,支持压缩解压使用密码。
Android端zip压缩与解压,目前暂时只做zip格式支持,基于Zip4j (http://www.lingala.net/zip4j/)进行扩展成工具类,支持对单个文件,多个文件以及文件夹进行压缩,对压缩文件解压到到指定目录,支持压缩解压使用密码...
android Zip压缩 删除文件 Gson解析Json
其实在网上有很多介绍下载文件或者解压zip文件的文章,但是两者结合的不多,所以这篇文章在此记录一下下载zip文件并直接解压的方法,直接上代码,文末有源码下载。 下载: import java.io.BufferedInputStream; ...
主要为大家详细介绍了Android实现文件或文件夹压缩成.zip格式压缩包,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android ZIP文件下载以及解压.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
解决android 自带zip解压缩中文乱码的问题,导入zip.jar即可使用(与系统提供的用法相同),大小仅24k
android Zip解压、压缩 demo
(安卓APP项目源代码)文件管理器源码,文件拖曳,list弹性,root ,zip压缩解.zip(安卓APP项目源代码)文件管理器源码,文件拖曳,list弹性,root ,zip压缩解.zip(安卓APP项目源代码)文件管理器源码,文件拖曳,list弹性,...
javaandroid可用的ziprar解压缩代码实现.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
NULL 博文链接:https://technicalsearch.iteye.com/blog/2158795
主要介绍了Android实现zip文件压缩及解压缩的方法,涉及Android针对文件的遍历及zip压缩与解压缩的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
zip压缩/解压,由于只用utf-8字符,所以用windows系统的winrar的文件会出现中文乱码. 标签页下面是路径栏,点击路径栏相应的目录名,会跳到对应的目录下,长按路径栏可以直接输入路径。 显示文件有列表和图标两种...
基于zip4j开源框架实现android解压带有密码的zip文件(进度条显示),项目中有jar包,非常好用。解压、压缩、压缩添加密码、解压带有密码的zip都可以使用。
android图像识别,采用百度sdk,识别准确率基本上能用,有需求的可自行下载,需要申请百度sdk的key
NULL 博文链接:https://hzy3774.iteye.com/blog/1704419
zip4j -- Java处理zip压缩文件的完整解决...支持文件压缩、解压并加密解密,不过只适用于zip文件格式。rar文件不支持。jar包的局限性。但是我下载了支持rar加密解密的jar包。你可以自己尝试。如有疑问可以留言。谢谢!
单纯对图片进行裁切,压缩已经有很多文章介绍。但是裁切成多少,压缩成多少却很难控制好,裁切过头图片太小,质量压缩过头则显示效果太差。于是自然想到app巨头“微信”会是怎么处理,Luban(鲁班)就是通过在微信朋友...
本例子是一个支持解压缩的安卓文件管理器源码,另外还可以进行常规的复制移动删除重命名等操作,压缩文件的时候只能压缩成zip格式,不支持rar格式,解压缩也是紧支持zip格式,源码有比较详细的注释,需要的朋友可以...
相关推荐
Android端zip压缩与解压,目前暂时只做zip格式支持,基于Zip4j 进行扩展成工具类,支持对单个文件,多个文件以及文件夹进行压缩,对压缩文件解压到到指定目录,支持压缩解压使用密码。
Android端zip压缩与解压,目前暂时只做zip格式支持,基于Zip4j (http://www.lingala.net/zip4j/)进行扩展成工具类,支持对单个文件,多个文件以及文件夹进行压缩,对压缩文件解压到到指定目录,支持压缩解压使用密码...
android Zip压缩 删除文件 Gson解析Json
其实在网上有很多介绍下载文件或者解压zip文件的文章,但是两者结合的不多,所以这篇文章在此记录一下下载zip文件并直接解压的方法,直接上代码,文末有源码下载。 下载: import java.io.BufferedInputStream; ...
主要为大家详细介绍了Android实现文件或文件夹压缩成.zip格式压缩包,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android ZIP文件下载以及解压.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
解决android 自带zip解压缩中文乱码的问题,导入zip.jar即可使用(与系统提供的用法相同),大小仅24k
android Zip解压、压缩 demo
(安卓APP项目源代码)文件管理器源码,文件拖曳,list弹性,root ,zip压缩解.zip(安卓APP项目源代码)文件管理器源码,文件拖曳,list弹性,root ,zip压缩解.zip(安卓APP项目源代码)文件管理器源码,文件拖曳,list弹性,...
javaandroid可用的ziprar解压缩代码实现.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
NULL 博文链接:https://technicalsearch.iteye.com/blog/2158795
主要介绍了Android实现zip文件压缩及解压缩的方法,涉及Android针对文件的遍历及zip压缩与解压缩的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
zip压缩/解压,由于只用utf-8字符,所以用windows系统的winrar的文件会出现中文乱码. 标签页下面是路径栏,点击路径栏相应的目录名,会跳到对应的目录下,长按路径栏可以直接输入路径。 显示文件有列表和图标两种...
基于zip4j开源框架实现android解压带有密码的zip文件(进度条显示),项目中有jar包,非常好用。解压、压缩、压缩添加密码、解压带有密码的zip都可以使用。
android图像识别,采用百度sdk,识别准确率基本上能用,有需求的可自行下载,需要申请百度sdk的key
NULL 博文链接:https://hzy3774.iteye.com/blog/1704419
zip4j -- Java处理zip压缩文件的完整解决...支持文件压缩、解压并加密解密,不过只适用于zip文件格式。rar文件不支持。jar包的局限性。但是我下载了支持rar加密解密的jar包。你可以自己尝试。如有疑问可以留言。谢谢!
单纯对图片进行裁切,压缩已经有很多文章介绍。但是裁切成多少,压缩成多少却很难控制好,裁切过头图片太小,质量压缩过头则显示效果太差。于是自然想到app巨头“微信”会是怎么处理,Luban(鲁班)就是通过在微信朋友...
本例子是一个支持解压缩的安卓文件管理器源码,另外还可以进行常规的复制移动删除重命名等操作,压缩文件的时候只能压缩成zip格式,不支持rar格式,解压缩也是紧支持zip格式,源码有比较详细的注释,需要的朋友可以...