Android實現(xiàn)文件壓縮與解壓工具類
更新時間:2024年04月24日 09:40:44 作者:generallizhong
這篇文章主要為大家詳細介紹了如何使用Android實現(xiàn)一個文件壓縮與解壓工具類,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
一個簡單壓縮解壓工具類
public class ZipUtils { public static void compressFolder(Activity activity, String sourcePath, String zipFile) throws IOException { File folder = new File(sourcePath); File zipPath = new File(zipFile); if (zipPath.exists()) { zipPath.delete(); } // 創(chuàng)建輸出流并指定要生成的ZIP文件路徑 try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) { addFilesToZip(folder, folder.getName(), zos); System.out.println("文件夾已成功壓縮為 " + zipFile); Toast.makeText(activity, "壓縮已完成", Toast.LENGTH_SHORT).show(); } catch (IOException e) { throw new RuntimeException("無法將文件夾壓縮到ZIP文件中", e); } } private static void addFilesToZip(File file, String parentName, ZipOutputStream zos) throws IOException { if (!file.exists()) return; for (File child : file.listFiles()) { if (child.isDirectory()) { addFilesToZip(child, parentName + "/" + child.getName(), zos); } else { byte[] buffer = new byte[1024]; // 獲取當(dāng)前文件相對于源文件夾的路徑名稱 String entryName = parentName + "/" + child.getName(); // 添加新條目到ZIP文件中 zos.putNextEntry(new ZipEntry(entryName)); // 讀取文件內(nèi)容并寫入ZIP文件 try (InputStream is = new FileInputStream(child)) { int length; while ((length = is.read(buffer)) > 0) { zos.write(buffer, 0, length); } } finally { zos.closeEntry(); } } } } public static void Unzip(String zipFile, String targetDir) { Log.e("lz>>","zipFile:"+zipFile+" targetDir:"+targetDir); int BUFFER = 4096; //這里緩沖區(qū)我們使用4KB, String strEntry; //保存每個zip的條目名稱 try { BufferedOutputStream dest = null; //緩沖輸出流 FileInputStream fis = new FileInputStream(zipFile); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry; //每個zip條目的實例 while ((entry = zis.getNextEntry()) != null) { try { Log.i("Unzip: ","="+ entry); int count; byte data[] = new byte[BUFFER]; strEntry = entry.getName(); File entryFile = new File(targetDir + strEntry); File entryDir = new File(entryFile.getParent()); if (!entryDir.exists()) { entryDir.mkdirs(); } FileOutputStream fos = new FileOutputStream(entryFile); dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } catch (Exception ex) { ex.printStackTrace(); } } zis.close(); Log.i("Unzip: ","="+ "結(jié)束"); } catch (Exception cwj) { cwj.printStackTrace(); } }
壓縮使用:
File appDir = getFilesDir(); String filePath = appDir+“”";//需要壓縮的文件路徑 String zipWalletfile = zipfile.getAbsolutePath(); try { //zipWalletfile + "/wallet.zip"壓縮后的文件名 ZipUtils.compressFolder(ZipActivity.this, filePath , zipWalletfile + "/wallet.zip"); } catch (Exception e) { throw new RuntimeException(e); }
解壓使用:
File zipfile = Environment.getExternalStorageDirectory(); String zipWalletfile = zipfile.getAbsolutePath();//待解壓文件 String unzipPath=getFilesDir().getPath();解壓文件路徑 ZipUtils.Unzip(zipWalletfile + "/wallet.zip",unzipPath+"/");
到此這篇關(guān)于Android實現(xiàn)文件壓縮與解壓工具類的文章就介紹到這了,更多相關(guān)Android文件壓縮解壓內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android?studio實現(xiàn)日期?、時間選擇器與進度條
這篇文章主要為大家詳細介紹了Android?studio實現(xiàn)日期、時間選擇器與進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01Android頁面之間進行數(shù)據(jù)回傳的方法分析
這篇文章主要介紹了Android頁面之間進行數(shù)據(jù)回傳的方法,結(jié)合實例形式分析了Android頁面之間進行數(shù)據(jù)的傳遞與處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06Android開發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙詳細介紹與實例
這篇文章主要介紹了Android開發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙詳細介紹與實例,有需要的朋友可以參考一下2013-12-12android 完全退出應(yīng)用程序?qū)崿F(xiàn)代碼
這篇文章主要介紹了在android中完全退出應(yīng)用的實現(xiàn)代碼,多種實現(xiàn)方法,大家可以根據(jù)需求選擇2013-06-06Eclipse NDK遷移到Android Studio的方法示例
本篇文章主要介紹了Eclipse NDK遷移到Android Studio的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03