package com.fxy.utils;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CompressUtil {

    private static final String TAG = "CompressUtil";

    public static void compressFile(File sourceFile, File targetFile) {
        try {
            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(targetFile));
            FileInputStream fileInput = new FileInputStream(sourceFile);

            ZipEntry zipEntry = new ZipEntry(sourceFile.getName());
            zipOut.putNextEntry(zipEntry);

            byte[] bytes = new byte[1024];
            int length;

            while ((length = fileInput.read(bytes)) >= 0) {
                zipOut.write(bytes, 0, length);
            }

            zipOut.closeEntry();
            fileInput.close();
            zipOut.close();

            Log.i(TAG, "File compressed successfully.");

        } catch (IOException e) {
            Log.e(TAG, "Error compressing file.", e);
        }
    }
}
