栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android实现zip文件压缩及解压缩的方法

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android实现zip文件压缩及解压缩的方法

本文实例讲述了Android实现zip文件压缩及解压缩的方法。分享给大家供大家参考。具体如下:

DirTraversal.java如下:

package com.once;
import java.io.File;
import java.util.ArrayList;
import java.util.linkedList;

public class DirTraversal {
  //no recursion
  public static linkedList listlinkedFiles(String strPath) {
    linkedList list = new linkedList();
    File dir = new File(strPath);
    File file[] = dir.listFiles();
    for (int i = 0; i < file.length; i++) {
      if (file[i].isDirectory())
 list.add(file[i]);
      else
 System.out.println(file[i].getAbsolutePath());
    }
    File tmp;
    while (!list.isEmpty()) {
      tmp = (File) list.removeFirst();
      if (tmp.isDirectory()) {
 file = tmp.listFiles();
 if (file == null)
   continue;
 for (int i = 0; i < file.length; i++) {
   if (file[i].isDirectory())
     list.add(file[i]);
   else
     System.out.println(file[i].getAbsolutePath());
 }
      } else {
 System.out.println(tmp.getAbsolutePath());
      }
    }
    return list;
  }
  //recursion
  public static ArrayList listFiles(String strPath) {
    return refreshFileList(strPath);
  }
  public static ArrayList refreshFileList(String strPath) {
    ArrayList filelist = new ArrayList();
    File dir = new File(strPath);
    File[] files = dir.listFiles();
    if (files == null)
      return null;
    for (int i = 0; i < files.length; i++) {
      if (files[i].isDirectory()) {
 refreshFileList(files[i].getAbsolutePath());
      } else {
 if(files[i].getName().toLowerCase().endsWith("zip"))
   filelist.add(files[i]);
      }
    }
    return filelist;
  }
}

ZipUtils.java如下:

package com.once;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
  private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte
  
  public static void zipFiles(Collection resFileList, File zipFile) throws IOException {
    ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
 zipFile), BUFF_SIZE));
    for (File resFile : resFileList) {
      zipFile(resFile, zipout, "");
    }
    zipout.close();
  }
  
  public static void zipFiles(Collection resFileList, File zipFile, String comment)
      throws IOException {
    ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
 zipFile), BUFF_SIZE));
    for (File resFile : resFileList) {
      zipFile(resFile, zipout, "");
    }
    zipout.setComment(comment);
    zipout.close();
  }
  
  public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
    File desDir = new File(folderPath);
    if (!desDir.exists()) {
      desDir.mkdirs();
    }
    ZipFile zf = new ZipFile(zipFile);
    for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
      ZipEntry entry = ((ZipEntry)entries.nextElement());
      InputStream in = zf.getInputStream(entry);
      String str = folderPath + File.separator + entry.getName();
      str = new String(str.getBytes("8859_1"), "GB2312");
      File desFile = new File(str);
      if (!desFile.exists()) {
 File fileParentDir = desFile.getParentFile();
 if (!fileParentDir.exists()) {
   fileParentDir.mkdirs();
 }
 desFile.createNewFile();
      }
      OutputStream out = new FileOutputStream(desFile);
      byte buffer[] = new byte[BUFF_SIZE];
      int realLength;
      while ((realLength = in.read(buffer)) > 0) {
 out.write(buffer, 0, realLength);
      }
      in.close();
      out.close();
    }
  }
  
  public static ArrayList upZipSelectedFile(File zipFile, String folderPath,
      String nameContains) throws ZipException, IOException {
    ArrayList fileList = new ArrayList();
 
    File desDir = new File(folderPath);
    if (!desDir.exists()) {
      desDir.mkdir();
    }
    ZipFile zf = new ZipFile(zipFile);
    for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
      ZipEntry entry = ((ZipEntry)entries.nextElement());
      if (entry.getName().contains(nameContains)) {
 InputStream in = zf.getInputStream(entry);
 String str = folderPath + File.separator + entry.getName();
 str = new String(str.getBytes("8859_1"), "GB2312");
 // str.getBytes("GB2312"),"8859_1" 输出
 // str.getBytes("8859_1"),"GB2312" 输入
 File desFile = new File(str);
 if (!desFile.exists()) {
   File fileParentDir = desFile.getParentFile();
   if (!fileParentDir.exists()) {
     fileParentDir.mkdirs();
   }
   desFile.createNewFile();
 }
 OutputStream out = new FileOutputStream(desFile);
 byte buffer[] = new byte[BUFF_SIZE];
 int realLength;
 while ((realLength = in.read(buffer)) > 0) {
   out.write(buffer, 0, realLength);
 }
 in.close();
 out.close();
 fileList.add(desFile);
      }
    }
    return fileList;
  }
  
  public static ArrayList getEntriesNames(File zipFile) throws ZipException, IOException {
    ArrayList entryNames = new ArrayList();
    Enumeration entries = getEntriesEnumeration(zipFile);
    while (entries.hasMoreElements()) {
      ZipEntry entry = ((ZipEntry)entries.nextElement());
      entryNames.add(new String(getEntryName(entry).getBytes("GB2312"), "8859_1"));
    }
    return entryNames;
  }
  
  public static Enumeration getEntriesEnumeration(File zipFile) throws ZipException,
      IOException {
    ZipFile zf = new ZipFile(zipFile);
    return zf.entries();
  }
  
  public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException {
    return new String(entry.getComment().getBytes("GB2312"), "8859_1");
  }
  
  public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException {
    return new String(entry.getName().getBytes("GB2312"), "8859_1");
  }
  
  private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)
      throws FileNotFoundException, IOException {
    rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator)
 + resFile.getName();
    rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
    if (resFile.isDirectory()) {
      File[] fileList = resFile.listFiles();
      for (File file : fileList) {
 zipFile(file, zipout, rootpath);
      }
    } else {
      byte buffer[] = new byte[BUFF_SIZE];
      BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile),
   BUFF_SIZE);
      zipout.putNextEntry(new ZipEntry(rootpath));
      int realLength;
      while ((realLength = in.read(buffer)) != -1) {
 zipout.write(buffer, 0, realLength);
      }
      in.close();
      zipout.flush();
      zipout.closeEntry();
    }
  }
}

希望本文所述对大家的Android程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/164285.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号