An overview,
In this article, I will show you how to zip zip files and how to unzip zip packages. All of this is done using the core library java.util.zip that Java provides.
Second, compress files
First let’s look at a simple example – compressing a single file. TXT file to a zip file named Compressed. Zip.
public class ZipFile {
public static void main(String[] args) throws IOException {
// Output compressed package
FileOutputStream fos = new FileOutputStream("src/main/resources/compressed.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
// Compressed file
File fileToZip = new File("src/main/resources/test1.txt");
FileInputStream fis = new FileInputStream(fileToZip);
// Add files to the compressed package
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length); } zipOut.close(); fis.close(); fos.close(); }}Copy the code
Three, compress multiple files
Next, let’s look at how to compress multiple files into a single ZIP file. TXT and test2. TXT to multiCompressed. Zip:
public class ZipMultipleFiles {
public static void main(String[] args) throws IOException {
List<String> srcFiles = Arrays.asList("src/main/resources/test1.txt"."src/main/resources/test2.txt");
FileOutputStream fos = new FileOutputStream("src/main/resources/multiCompressed.zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
// Add multiple files to the compressed package
for (String srcFile : srcFiles) {
File fileToZip = new File(srcFile);
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length); } fis.close(); } zipOut.close(); fos.close(); }}Copy the code
4. Compress the directory
In the following example, we compress the zipTest directory and its recursive subdirectory files into dirCompressed.zip:
public class ZipDirectory { public static void main(String[] args) throws IOException, FileNotFoundException {/ / the compressed folder String sourceFile = "SRC/main/resources/zipTest"; / / compression results output, i.e., zip FileOutputStream fos = new FileOutputStream (" SRC/main/resources/dirCompressed.zip "); ZipOutputStream zipOut = new ZipOutputStream(fos); File fileToZip = new File(sourceFile); ZipFile (fileToZip, filetozip.getName (), zipOut); // Close the output stream zipout.close (); fos.close(); } /** * Recursively compresses the fileToZip folder and its subdirectories into a zip file * @param fileToZip recursively compresses the current object, which may be a folder, It can also be a file * @param fileName fileToZip file or folder name * @param zipOut output stream of compressed files * @throws IOException */ private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {// Uncompress hidden folder if (fileTozip.ishidden ()) {return; If (filetozip.isdirectory ()) {if (filename.endswith ("/")) {// If (filename.endswith ("/")) { Put the folder into the zipOut compression output stream zipout.putNextentry (new ZipEntry(fileName)) as a compression box; zipOut.closeEntry(); PutNextEntry (new ZipEntry(fileName + "/")); zipOut.closeEntry(); } // zipFile File[] children = fileTozip.listfiles (); for (File childFile : children) { zipFile(childFile, fileName + "/" + childFile.getName(), zipOut); } // If the current recursive object is a folder, return after adding ZipEntry; FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileName); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); } fis.close(); }}Copy the code
- To compress subdirectories and their subdirectory files, recursive traversal is required
- Each time the traversal finds a directory, we append its name with a “/” and end it withZipEntrySave to a compressed package to preserve the compressed directory structure.
- Each time the traversal finds a file, it is compressed into a compressed package in bytecode form
5. Decompress the ZIP package
The following describes how to decompress the ZIP package. In this example, we unzip compressed compressed compressed.zip into a new folder named unzipTest.
Public class UnzipFile {public static void main(String[] args) throws IOException {// Decompressed compressed file String fileZip = "src/main/resources/unzipTest/compressed.zip"; / / target directory File destDir = new File (" SRC/main/resources/unzipTest "); byte[] buffer = new byte[1024]; ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip)); ZipEntry = zis.getNextentry (); ZipEntry = zis.getNextentry (); while (zipEntry ! = null) { File newFile = newFile(destDir, zipEntry); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); ZipEntry = zis.getNextentry (); } zis.closeEntry(); zis.close(); } // Unzip the target folder, Public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException { File destFile = new File(destinationDir, zipEntry.getName()); String destDirPath = destinationDir.getCanonicalPath(); String destFilePath = destFile.getCanonicalPath(); if (! DestFilePath. StartsWith (destDirPath + file.separator)) {throw new IOException(" + zipentry.getName ()); } return destFile; }}Copy the code
Welcome to my blog, where there are many fine collections
- This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.
Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.
- Spring Boot2.0 by Hand
- Spring Security- JWT-OAUTH2
- RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
- “Actual SpringCloud Micro-service from Bronze to King”
- VUE Series