SoFunction
Updated on 2025-04-07

Android file compression and decompression tools

A simple compression decompression tool class

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 (()) {
            ();
        }
 
        // Create an output stream and specify the path to the ZIP file to be generated        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
            addFilesToZip(folder, (), zos);
 
            ("The folder has been successfully compressed to " + zipFile);
            (activity, "Compression is complete", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            throw new RuntimeException("Cannot compress folder into ZIP file", e);
        }
    }
 
    private static void addFilesToZip(File file, String parentName, ZipOutputStream zos) throws IOException {
        if (!()) return;
 
        for (File child : ()) {
            if (()) {
                addFilesToZip(child, parentName + "/" + (), zos);
            } else {
                byte[] buffer = new byte[1024];
 
                // Get the path name of the current file relative to the source folder                String entryName = parentName + "/" + ();
 
                // Add new entry to ZIP file                (new ZipEntry(entryName));
 
                // Read the file content and write to the ZIP file                try (InputStream is = new FileInputStream(child)) {
                    int length;
                    while ((length = (buffer)) > 0) {
                        (buffer, 0, length);
                    }
                } finally {
                    ();
                }
            }
        }
    }
 
    public static void Unzip(String zipFile, String targetDir) {
        ("lz>>","zipFile:"+zipFile+" targetDir:"+targetDir);
        int BUFFER = 4096; //We use 4KB of the buffer here.        String strEntry; //Save the entry name of each zip 
        try {
            BufferedOutputStream dest = null; //Buffer output stream            FileInputStream fis = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry; //Instance of each zip entry 
            while ((entry = ()) != null) {
 
                try {
                    ("Unzip: ","="+ entry);
                    int count;
                    byte data[] = new byte[BUFFER];
                    strEntry = ();
 
                    File entryFile = new File(targetDir + strEntry);
                    File entryDir = new File(());
                    if (!()) {
                        ();
                    }
 
                    FileOutputStream fos = new FileOutputStream(entryFile);
                    dest = new BufferedOutputStream(fos, BUFFER);
                    while ((count = (data, 0, BUFFER)) != -1) {
                        (data, 0, count);
                    }
                    ();
                    ();
                } catch (Exception ex) {
                    ();
                }
            }
            ();
            ("Unzip: ","="+ "Finish");
        } catch (Exception cwj) {
            ();
        }
    }

Compression use:

               File appDir = getFilesDir();
                String filePath = appDir+“”";//The file path that needs to be compressed                String zipWalletfile = ();
                try {
                //zipWalletfile + "/" compressed file name                    (, filePath , zipWalletfile + "/");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

Decompression:

   File zipfile = ();
        String zipWalletfile = ();//The file to be decompressed        String unzipPath=getFilesDir().getPath();Unzip the file path
        (zipWalletfile + "/",unzipPath+"/");

This is the article about Android file compression and decompression tools. For more related content on Android file compression and decompression, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!