android文件或文件夹压缩
private static final int BUFFER = 1024 * 1024; /** * 压缩目录 * * @param File * dir * @param ZipOutputStream * out * @param String * basedir */ private void compress2Directory(File dir, ZipOutputStream out, String basedir) { if (!dir.exists()) return; File[] files = dir.listFiles(); if(files!=null && files.length !=0){ for (int i = 0; i < files.length; i++) { compress(files[i], out, basedir + dir.getName() + "/"); } } } /** * 压缩文件 * * @param File * file * @param ZipOutputStream * out * @param String * basedir */ private void compress2File(File file, ZipOutputStream out, String basedir) { if (!file.exists()) { return; } BufferedInputStream bis = null; try { bis = new BufferedInputStream( new FileInputStream(file)); ZipEntry entry = new ZipEntry(basedir + file.getName()); out.putNextEntry(entry); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } } catch (Exception e) { Write.debug(""+e.getMessage()); }finally { try { bis.close(); } catch (IOException e) { Write.debug(""+e.getMessage()); } } } public void compress(File file, ZipOutputStream out, String basedir) { if (file.isFile()) { this.compress2File(file, out, basedir); } else { this.compress2Directory(file, out, basedir); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。