標籤:檔案壓縮 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);}}
android檔案或檔案夾壓縮