본문 바로가기

개발

자바 JAVA ZIP UNZIP 압축파일 압축, 해제, 풀기, 비밀번호 파일 풀기 등의 소스와 방법

자바 JAVA ZIP UNZIP 압축파일 압축, 해제, 풀기, 비밀번호 파일 풀기 등의 소스와 방법

1) 표준 라이브러리 이용
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
public class DeCompressZipFileExample {
 
    public static void main(String[] args) throws Exception{
        String zipFile = "C:\\FileforJava\\zip_compressed_file.zip";
        String outputFolder = "C:\\FileforJava\\Unziped\\";
 
        System.out.println("Begin unzip "+ zipFile + " into "+outputFolder);
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();
        while(ze!=null){
            String entryName = ze.getName();
            System.out.print("Extracting " + entryName + " -> " + outputFolder + File.separator +  entryName + "...");
            File f = new File(outputFolder + File.separator +  entryName);
            //create all folder needed to store in correct relative path.
            f.getParentFile().mkdirs();
            FileOutputStream fos = new FileOutputStream(f);
            int len;
            byte buffer[] = new byte[1024];
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();  
            System.out.println("OK!");
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
 
        System.out.println( zipFile + " unzipped successfully");
    }
 
}

2) zip4j 이용 : http://www.lingala.net/zip4j/
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.core.ZipFile;

public static void unzip(){
    String source = "some/compressed/file.zip";
    String destination = "some/destination/folder";
    String password = "password";

    try {
         ZipFile zipFile = new ZipFile(source);
         if (zipFile.isEncrypted()) {
            zipFile.setPassword(password);
         }
         zipFile.extractAll(destination);
    } catch (ZipException e) {
        e.printStackTrace();
    }
}

참고) The Maven dependency is:
<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>1.3.2</version>
</dependency>

3) Apache Commons 이용 : http://commons.apache.org/
public class ZipUtils {
    Logger log;

    public ZipUtils(Logger log) {
        this.log = log;       
    }

    public void unzipArchive(File archive, File outputDir) {
        try {
            ZipFile zipfile = new ZipFile(archive);
            for (Enumeration e = zipfile.entries(); e.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                unzipEntry(zipfile, entry, outputDir);
            }
        } catch (Exception e) {
            log.error("Error while extracting file " + archive, e);
        }
    }

    private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {


        if (entry.isDirectory()) {
            createDir(new File(outputDir, entry.getName()));
            return;
        }
       
        File outputFile = new File(outputDir, entry.getName());
        if (!outputFile.getParentFile().exists()){
            createDir(outputFile.getParentFile());
        }

        log.debug("Extracting: " + entry);
        BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

        try {
            IOUtils.copy(inputStream, outputStream);
        } finally {
            outputStream.close();
            inputStream.close();
        }
    }

    private void createDir(File dir) {
        log.debug("Creating dir "+dir.getName());
        if(!dir.mkdirs()) throw new RuntimeException("Can not create dir "+dir);
    }
}