Android 解压缩功能
主要用到zip:
1 import java.util.Enumeration; 2 import java.util.zip.CRC32; 3 import java.util.zip.CheckedOutputStream; 4 import java.util.zip.ZipEntry; 5 import java.util.zip.ZipFile; 6 import java.util.zip.ZipOutputStream;
解压文件:
1 /** 2 * 解压缩功能. 将zipPath文件 解压到folderPath目录下. 3 * 4 * @param zipFile zip文件 5 * @param destDir 解压到的文件路径 6 * @throws java.io.IOException 7 */ 8 public static void upZipFile(File zipFile, String destDir) { 9 File pathFile = new File(destDir + "/"); 10 if (!pathFile.exists()) { 11 pathFile.mkdirs(); 12 } 13 14 try { 15 ZipFile zip = new ZipFile(zipFile); 16 17 for (Enumeration<?> entries = zip.entries(); entries.hasMoreElements(); ) { 18 ZipEntry entry = (ZipEntry) entries.nextElement(); 19 String zipEntryName = entry.getName(); 20 21 InputStream in = zip.getInputStream(entry); 22 String outPath = (destDir + "/" + zipEntryName).replaceAll("\\*", "/"); 23 24 // 判断路径是否存在,不存在则创建文件路径 25 File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); 26 if (!file.exists()) { 27 file.mkdirs(); 28 } 29 // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压 30 if (new File(outPath).isDirectory()) { 31 continue; 32 } 33 // 输出文件路径信息 34 System.out.println(outPath); 35 36 OutputStream out = new FileOutputStream(outPath); 37 byte[] buf1 = new byte[1024]; 38 int len; 39 while ((len = in.read(buf1)) > 0) { 40 out.write(buf1, 0, len); 41 } 42 in.close(); 43 out.close(); 44 } 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 }
压缩文件:
(1)首先要声明个压缩类:
1 public static class ZipCompressor { 2 static final int BUFFER = 1024 * 128; 3 4 private File zipFile; 5 6 public ZipCompressor(String pathName) { 7 zipFile = new File(pathName); 8 } 9 10 public void compress(String srcPathName) { 11 File file = new File(srcPathName); 12 if (!file.exists()) 13 throw new RuntimeException(srcPathName + "不存在!"); 14 try { 15 FileOutputStream fileOutputStream = new FileOutputStream(zipFile); 16 CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); 17 ZipOutputStream out = new ZipOutputStream(cos); 18 String basedir = ""; 19 compress(file, out, basedir); 20 out.close(); 21 } catch (Exception e) { 22 throw new RuntimeException(e); 23 } 24 } 25 26 private void compress(File file, ZipOutputStream out, String basedir) { 27 /* 判断是目录还是文件 */ 28 if (file.isDirectory()) { 29 System.out.println("压缩:" + basedir + file.getName()); 30 this.compressDirectory(file, out, basedir); 31 } else { 32 System.out.println("压缩:" + basedir + file.getName()); 33 this.compressFile(file, out, basedir); 34 } 35 } 36 37 /** 压缩一个目录 */ 38 private void compressDirectory(File dir, ZipOutputStream out, String basedir) { 39 if (!dir.exists()) 40 return; 41 42 File[] files = dir.listFiles(); 43 for (int i = 0; i < files.length; i++) { 44 /* 递归 */ 45 compress(files[i], out, basedir + dir.getName() + "/"); 46 } 47 } 48 49 /** 压缩一个文件 */ 50 private void compressFile(File file, ZipOutputStream out, String basedir) { 51 if (!file.exists()) { 52 return; 53 } 54 try { 55 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); 56 ZipEntry entry = new ZipEntry(basedir + file.getName()); 57 out.putNextEntry(entry); 58 int count; 59 byte data[] = new byte[BUFFER]; 60 while ((count = bis.read(data, 0, BUFFER)) != -1) { 61 out.write(data, 0, count); 62 } 63 bis.close(); 64 } catch (Exception e) { 65 throw new RuntimeException(e); 66 } 67 } 68 }
压缩类使用:
1 public static void compress(String srcPathName, String zipPathName) { 2 ZipCompressor zipCompressor = new ZipCompressor(zipPathName); 3 zipCompresspr.compress(srcPathName); 4 }