Android Zip Unzip File zip(File srcFile, File targetFile)

Here you can find the source of zip(File srcFile, File targetFile)

Description

zip

Declaration

public static boolean zip(File srcFile, File targetFile) 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {

    public static boolean zip(File srcFile, File targetFile) {
        if (!srcFile.exists())
            return false;
        if (!targetFile.getParentFile().exists())
            targetFile.getParentFile().mkdir();
        try {/*  w w w.ja va 2s  . c o m*/
            FileInputStream localFileInputStream = new FileInputStream(
                    targetFile);
            ZipOutputStream localZipOutputStream = new ZipOutputStream(
                    new FileOutputStream(targetFile));
            byte[] arrayOfByte = new byte[1024];
            localZipOutputStream.putNextEntry(new ZipEntry(srcFile
                    .getName()));
            while (true) {
                int i = localFileInputStream.read(arrayOfByte);
                if (i <= 0) {
                    localZipOutputStream.closeEntry();
                    localFileInputStream.close();
                    localZipOutputStream.close();
                    return true;
                }
                localZipOutputStream.write(arrayOfByte, 0, i);
            }
        } catch (Exception localException) {
            localException.printStackTrace();
        }
        return false;
    }
}

Related

  1. zip(String[] files, String zipFile)
  2. zip(String[] files, String zipFile)
  3. zip(String[] files, String zipFile)
  4. zip(String[] files, String zipFile)
  5. zip(String[] files, String zipFile)
  6. zip(File[] _files, File _zipFile)
  7. zip(List listFiles, String destZipFilePath)
  8. zipFile(File source, String basePath, ZipOutputStream zos)
  9. zipFileAtPath(File sourceFile, String toLocation)