您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了Android壓縮文件和文件夾的方法,供大家參考,具體內(nèi)容如下
/** * 壓縮文件和文件夾 * * @param srcFileString 要壓縮的文件或文件夾 * @param zipFileString 壓縮完成的Zip路徑 * @throws Exception */ public static void ZipFolder(String srcFileString, String zipFileString) throws Exception { //創(chuàng)建ZIP ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString)); //創(chuàng)建文件 File file = new File(srcFileString); //壓縮 LogUtils.LOGE("---->"+file.getParent()+"==="+file.getAbsolutePath()); ZipFiles(file.getParent()+ File.separator, file.getName(), outZip); //完成和關(guān)閉 outZip.finish(); outZip.close(); } /** * 壓縮文件 * * @param folderString * @param fileString * @param zipOutputSteam * @throws Exception */ private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) throws Exception { LogUtils.LOGE("folderString:" + folderString + "\n" + "fileString:" + fileString + "\n=========================="); if (zipOutputSteam == null) return; File file = new File(folderString + fileString); if (file.isFile()) { ZipEntry zipEntry = new ZipEntry(fileString); FileInputStream inputStream = new FileInputStream(file); zipOutputSteam.putNextEntry(zipEntry); int len; byte[] buffer = new byte[4096]; while ((len = inputStream.read(buffer)) != -1) { zipOutputSteam.write(buffer, 0, len); } zipOutputSteam.closeEntry(); } else { //文件夾 String fileList[] = file.list(); //沒有子文件和壓縮 if (fileList.length <= 0) { ZipEntry zipEntry = new ZipEntry(fileString + File.separator); zipOutputSteam.putNextEntry(zipEntry); zipOutputSteam.closeEntry(); } //子文件和遞歸 for (int i = 0; i < fileList.length; i++) { ZipFiles(folderString+fileString+"/", fileList[i], zipOutputSteam); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。