Android實(shí)現(xiàn)文件壓縮與解壓工具類
更新時(shí)間:2024年04月24日 09:40:44 作者:generallizhong
這篇文章主要為大家詳細(xì)介紹了如何使用Android實(shí)現(xiàn)一個文件壓縮與解壓工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(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條目的實(shí)例
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實(shí)現(xiàn)文件壓縮與解壓工具類的文章就介紹到這了,更多相關(guān)Android文件壓縮解壓內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Studio打包APK文件具體實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Android Studio打包APK文件具體實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Android使用WebSocket實(shí)現(xiàn)多人游戲
WebSocket 是 HTML5 一種新的協(xié)議。實(shí)現(xiàn)了瀏覽器與服務(wù)器全雙工通信,下面通過本文給大家分享Android使用WebSocket實(shí)現(xiàn)多人游戲,需要的朋友參考下吧2017-11-11
Android編程實(shí)現(xiàn)簡單流量管理功能實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)簡單流量管理功能的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)流量監(jiān)控所涉及的功能模塊與布局技巧,需要的朋友可以參考下2016-02-02
Android開發(fā)之菜單(menu)用法實(shí)例分析
這篇文章主要介紹了Android開發(fā)之菜單(menu)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android菜單的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android 拍照并對照片進(jìn)行裁剪和壓縮實(shí)例詳解
這篇文章主要介紹了Android 拍照并對照片進(jìn)行裁剪和壓縮實(shí)例詳解的相關(guān)資料,這里提供實(shí)例代碼,需要的朋友可以參考下2017-07-07

