📜  Java的ZIP API

📅  最后修改于: 2022-05-13 01:54:30.750000             🧑  作者: Mango

Java的ZIP API

Zip 文件是用于通过 Internet 传输文件的压缩文件。 Zip 和 unzip 是编写应用程序时非常重要的操作。 Java提供了丰富的 API 来压缩和解压缩文件。通过在Java使用 ZIP API,我们可以将压缩和解压缩为标准的 ZIP 和 GZIP 格式。

与往常一样,我们将讨论构造函数而不是进一步实现 ZIp API 的方法。让我们从构造函数开始,如下所示:

1. 默认构造函数:创建具有指定名称的 Zip 条目,并将字符串名称作为参数。

ZipEntry() {}

2. 参数化构造函数:使用指定文件创建新的 zip 条目

2.1 ZipEntry e



ZipEntry(e) {}

2.2文件文件:用于打开和读取指定文件对象的 ZIP 文件

ZipFile(File) {}

2.3用于打开和读取指定文件对象的 ZIP 文件

ZipFile(File file, Charset charset) {{}

现在让我们讨论以下方法:

方法 1:getEntry():告诉指定名称的 zip 文件条目,如果未找到则返回 null。

句法:

getEntry()

返回类型:ZipEntry

参数:字符串名称

方法二:getInputStream():用于获取读取指定zip条目内容的输入流。



句法:

getNextEntry()

返回类型输入流

参数:ZipEntry zipEntry

方法 3: putNextentry():开始写入一个新的 ZIP 文件条目。然后它将流定位到条目数据的开头。

句法:

putNextentry() {}

返回类型:无效

参数:ZipEntry e

执行:

示例 1

Java
// Java Program to Illustrate ZIP API
// To Zip a File 
  
// Importing required classes
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
  
// Class 1
// GfgZipDemo
public class GFG {
  
    // Main driver method
    public static void main(String[] argv)
    {
  
        // Calling method 2 inside main()
        GfgZipDemo.zip_one_file();
    }
  
    // Method 2
    // To perform ZIP on files
    public static void zip_one_file()
    {
  
        // Creating a byte array
        byte[] buffer = new byte[1024];
  
        // Try block to check for exceptions
        try {
  
            // Creating outputstream for writing the
            // information For ex c:\\desktop
            FileOutputStream fos = new FileOutputStream(
                "DESTINATION_PATH_WHERE_YOU_WANT_YOUR_ZIP");
  
            // Createing zip outputstream for zipping the
            // file by creating objects of ZipEntry and
            // ZipOutputStream classes
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze = new ZipEntry("testing1.txt");
  
            zos.putNextEntry(ze);
  
            // For ex c:\\desktop\\abc.txt
            FileInputStream in
                = new FileInputStream("FILE_TO_ZIP_PATH");
  
            // Looping
            int lengths;
            // Holds true till there is something in byte
            // array as declared above of size 1024
            while ((lengths = in.read(buffer)) > 0) {
  
                // Write
                zos.write(buffer, 0, lengths);
            }
  
            // Closing file connections using close() method
            // Closing entries using closeEntry() method
            in.close();
            zos.closeEntry();
            zos.close();
  
            // Print message on console to illustrate
            // successful execution of program
            System.out.println("Successfully compiled and executed.");
        }
  
        // Catch block to handle exceptions
        catch (IOException ex) {
  
            // Display the exceptions along with line number
            // using printStackTrace() method
            ex.printStackTrace();
        }
    }
}


Java
// Java Program to Illustrate ZIP API
// Where we are Unzipping a File
  
// Importing required classes
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
  
// Main class
// GfgUnZipDemo
public class GFG {
  
    // Member variables of classs
    List fileList;
    private static final String INPUT_ZIP_FILE
        = "YOUR_ZIP_FILE_PATH";
    // For ex c:\\desktop\\abc.zip
    private static final String OUTPUT_FOLDER
        = "YOUR_UNZIPPED_ZIP_FILE_PATH";
    // For ex c:\\desktop
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG unZip = new GFG();
        unZip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
    }
  
    // Method 2
    // To unzip a file
    public void unZipIt(String zipFile, String outputFolder)
    {
  
        // Creating a byte array
        byte[] buffer = new byte[1024];
  
        // Try block to check for exceptions
        try {
  
            // Creating output directory
            File folder = new File(OUTPUT_FOLDER);
  
            // If there is a folder
            if (!folder.exists()) {
                folder.mkdir();
            }
  
            // Than get the zip file
            ZipInputStream zis = new ZipInputStream(
                new FileInputStream(zipFile));
  
            // Getting the zipped list entry
            ZipEntry ze = zis.getNextEntry();
  
            // Till file is not fully unzipped
            while (ze != null) {
  
                String fileName = ze.getName();
                File newFile
                    = new File(outputFolder + File.separator
                               + fileName);
  
                System.out.println(
                    "file unzip : "
                    + newFile.getAbsoluteFile());
  
                // Create all non exists folders else we
                // will get FileNotFoundException for
                // compressed folder
                new File(newFile.getParent()).mkdirs();
  
                FileOutputStream fos
                    = new FileOutputStream(newFile);
  
                int len;
                // read till there are characters
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
  
                // Closing file output stream connections
                fos.close();
  
                ze = zis.getNextEntry();
            }
  
            // Closing all remaining connections
            zis.closeEntry();
            zis.close();
  
            // Display message for successful compilatuiona
            // nd run
            System.out.println(
                "Successfully compiled and executed.");
        }
  
        // Catch block to handle exceptions
        catch (IOException ex) {
  
            // Display the exception along with line number
            // using printStackTrace() method
            ex.printStackTrace();
        }
    }
}


输出:在输入路径后生成的控制台上,您将获得以下输出

Successfully compiled and executed.

从生成文件的图像可以看出如下媒体所示:

示例 2

Java

// Java Program to Illustrate ZIP API
// Where we are Unzipping a File
  
// Importing required classes
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
  
// Main class
// GfgUnZipDemo
public class GFG {
  
    // Member variables of classs
    List fileList;
    private static final String INPUT_ZIP_FILE
        = "YOUR_ZIP_FILE_PATH";
    // For ex c:\\desktop\\abc.zip
    private static final String OUTPUT_FOLDER
        = "YOUR_UNZIPPED_ZIP_FILE_PATH";
    // For ex c:\\desktop
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG unZip = new GFG();
        unZip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
    }
  
    // Method 2
    // To unzip a file
    public void unZipIt(String zipFile, String outputFolder)
    {
  
        // Creating a byte array
        byte[] buffer = new byte[1024];
  
        // Try block to check for exceptions
        try {
  
            // Creating output directory
            File folder = new File(OUTPUT_FOLDER);
  
            // If there is a folder
            if (!folder.exists()) {
                folder.mkdir();
            }
  
            // Than get the zip file
            ZipInputStream zis = new ZipInputStream(
                new FileInputStream(zipFile));
  
            // Getting the zipped list entry
            ZipEntry ze = zis.getNextEntry();
  
            // Till file is not fully unzipped
            while (ze != null) {
  
                String fileName = ze.getName();
                File newFile
                    = new File(outputFolder + File.separator
                               + fileName);
  
                System.out.println(
                    "file unzip : "
                    + newFile.getAbsoluteFile());
  
                // Create all non exists folders else we
                // will get FileNotFoundException for
                // compressed folder
                new File(newFile.getParent()).mkdirs();
  
                FileOutputStream fos
                    = new FileOutputStream(newFile);
  
                int len;
                // read till there are characters
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
  
                // Closing file output stream connections
                fos.close();
  
                ze = zis.getNextEntry();
            }
  
            // Closing all remaining connections
            zis.closeEntry();
            zis.close();
  
            // Display message for successful compilatuiona
            // nd run
            System.out.println(
                "Successfully compiled and executed.");
        }
  
        // Catch block to handle exceptions
        catch (IOException ex) {
  
            // Display the exception along with line number
            // using printStackTrace() method
            ex.printStackTrace();
        }
    }
}

输出:在输入路径后生成的控制台上,您将获得以下输出

Successfully compiled and executed.

从图片中可以看出,文件如下图所示: