java extract zip

http‮//:s‬www.lautturi.com
java extract zip

In Java, you can extract the contents of a zip file using the ZipInputStream class from the java.util.zip package. The ZipInputStream class allows you to read the entries of a zip file and extract them to a destination folder.

Here's an example of how to use the ZipInputStream class to extract a zip file in Java:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    public static void main(String[] args) {
        String zipFile = "path/to/zip/file.zip";
        String destination = "path/to/destination/folder";
        byte[] buffer = new byte[1024];

        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                String fileName = entry.getName();
                FileOutputStream fos = new FileOutputStream(destination + File.separator + fileName);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                zis.closeEntry();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, a ZipInputStream is created from a FileInputStream that reads the zip file. The getNextEntry() method is used to iterate over the entries of the zip file, and the read() method is used to read the contents of each entry into a buffer. The contents of the buffer are then written to a FileOutputStream that writes the extracted file to the destination folder.

This code will extract all of the entries of the zip file to the destination folder. You can use the ZipInputStream class to extract zip files in Java.

Created Time:2017-11-03 15:57:13  Author:lautturi