您好,登錄后才能下訂單哦!
本文實例為大家分享了Android壓縮文件和文件夾的方法,供大家參考,具體內容如下
/** * 壓縮文件和文件夾 * * @param srcFileString 要壓縮的文件或文件夾 * @param zipFileString 壓縮完成的Zip路徑 * @throws Exception */ public static void ZipFolder(String srcFileString, String zipFileString) throws Exception { //創建ZIP ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString)); //創建文件 File file = new File(srcFileString); //壓縮 LogUtils.LOGE("---->"+file.getParent()+"==="+file.getAbsolutePath()); ZipFiles(file.getParent()+ File.separator, file.getName(), outZip); //完成和關閉 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); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。