欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)文件壓縮與解壓工具類

 更新時(shí)間:2024年04月24日 09:40:44   作者:generallizhong  
這篇文章主要為大家詳細(xì)介紹了如何使用Android實(shí)現(xiàn)一個(gè)文件壓縮與解壓工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一個(gè)簡(jiǎn)單壓縮解壓工具類

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)前文件相對(duì)于源文件夾的路徑名稱
                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; //保存每個(gè)zip的條目名稱
 
        try {
            BufferedOutputStream dest = null; //緩沖輸出流
            FileInputStream fis = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry; //每個(gè)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android?studio實(shí)現(xiàn)日期?、時(shí)間選擇器與進(jìn)度條

    Android?studio實(shí)現(xiàn)日期?、時(shí)間選擇器與進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)日期、時(shí)間選擇器與進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法分析

    Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法分析

    這篇文章主要介紹了Android頁面之間進(jìn)行數(shù)據(jù)回傳的方法,結(jié)合實(shí)例形式分析了Android頁面之間進(jìn)行數(shù)據(jù)的傳遞與處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android 實(shí)現(xiàn)手機(jī)撥打電話的功能

    Android 實(shí)現(xiàn)手機(jī)撥打電話的功能

    本篇文章主要介紹 Android 開發(fā)手機(jī)撥打電話的功能,這里提供示例代碼,有興趣的小伙伴可以參考下
    2016-08-08
  • Android開發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙?jiān)敿?xì)介紹與實(shí)例

    Android開發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙?jiān)敿?xì)介紹與實(shí)例

    這篇文章主要介紹了Android開發(fā)學(xué)習(xí)之WallPaper設(shè)置壁紙?jiān)敿?xì)介紹與實(shí)例,有需要的朋友可以參考一下
    2013-12-12
  • android 完全退出應(yīng)用程序?qū)崿F(xiàn)代碼

    android 完全退出應(yīng)用程序?qū)崿F(xiàn)代碼

    這篇文章主要介紹了在android中完全退出應(yīng)用的實(shí)現(xiàn)代碼,多種實(shí)現(xiàn)方法,大家可以根據(jù)需求選擇
    2013-06-06
  • Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例

    Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例

    這篇文章主要介紹了Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例的相關(guān)資料,Android 圓角圖片的實(shí)現(xiàn)形式,包括用第三方、也有系統(tǒng),需要的朋友可以參考下
    2017-07-07
  • kotlin延遲初始化和密封類詳細(xì)講解

    kotlin延遲初始化和密封類詳細(xì)講解

    Kotlin語言的許多特性,包括變量不可變,變量不可為空,等等。這些特性都是為了盡可能地保證程序安全而設(shè)計(jì)的,但是有些時(shí)候這些特性也會(huì)在編碼時(shí)給我們帶來不少的麻煩,下面我們來了解延遲初始化和密封類的特點(diǎn)
    2022-11-11
  • Eclipse NDK遷移到Android Studio的方法示例

    Eclipse NDK遷移到Android Studio的方法示例

    本篇文章主要介紹了Eclipse NDK遷移到Android Studio的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • Android錄音并且輸出為Mp4文件的方法教程

    Android錄音并且輸出為Mp4文件的方法教程

    這篇文章主要給大家介紹了關(guān)于Android錄音并且輸出為Mp4文件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • 多面分析HarmonyOS與Android的特點(diǎn)

    多面分析HarmonyOS與Android的特點(diǎn)

    請(qǐng)教身邊的大佬們,公司的CTO、中臺(tái)部門的總監(jiān)、老東家數(shù)十年行業(yè)經(jīng)驗(yàn)的老架構(gòu)、以及在中科院讀研究生的大學(xué)老室友、技術(shù)圈的網(wǎng)友等等,他們都給出了自己獨(dú)特的看法,讓我從多方面更好的去了解到了大家對(duì)鴻蒙的認(rèn)識(shí)
    2021-08-08

最新評(píng)論