This example uses Java's Zip input and output stream to compress and decompress files. The previous part of the code implements the acquisition of file paths, changes to compressed file names, etc., as follows:
package com.utility.zip;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileOutputStream;import com.utility.io.IOUtil;/** *Compress and decompress files through Java's Zip input and output stream* * @author liujiduo * */public final class ZipUtil {private ZipUtil() {// empty}/** * Compressed file* * @param filePath * File path to be compressed* @return Compressed file*/public static File zip(String filePath) {File target = null;File source = new File(filePath);if (source.exists()) {// Compressed file name=source filename.zipString zipName = source.getName() + ".zip";target = new File(source.getParent(), zipName);if (target.exists()) {target.delete();// Delete the old file}FileOutputStream fos = null;ZipOutputStream zos = null;try {fos = new FileOutputStream(target);zos = new ZipOutputStream(new BufferedOutputStream(fos));// Add the corresponding file EntryaddEntry("/", source, zos);}catch (IOException e) {throw new RuntimeException(e);} finally {IOUtil.closeQuietly(zos, fos);}}return target;}/** * Scan to add file Entry * * @param base * Base path * @param source * Source file * @param zos * Zip file output stream* @throws IOException */private static void addEntry(String base, File source, ZipOutputStream zos) throws IOException {// Grade by directory, such as: /aaa/bbb.txtString entry = base + source.getName();if (source.isDirectory()) {for (File file : source.listFiles()) {// Recursively list all files in the directory and add files EntryaddEntry(entry + "/", file, zos);}} else {FileInputStream fis = null;BufferedInputStream bis = null;try {byte[] buffer = new byte[1024 * 10];fis = new FileInputStream(source);bis = new BufferedInputStream(fis, buffer.length);int read = 0;zos.putNextEntry(new ZipEntry(entry)); while ((read = bis.read(buffer, 0, buffer.length)) != -1) {zos.write(buffer, 0, read);}zos.closeEntry();} finally {IOUtil.closeQuietly(bis, fis);}}}/** * Unzip file* * @param filePath * Compress file path*/public static void unzip(String filePath) {File source = new File(filePath); if (source.exists()) {ZipInputStream zis = null;BufferedOutputStream bos = null;try {zis = new ZipInputStream(new FileInputStream(source));ZipEntry entry = null;while ((entry = zis.getNextEntry()) != null && !entry.isDirectory()) {File target = new File(source.getParent(), entry.getName());if (!target.getParentFile().exists()) {// Create the parent directory of the file target.getParentFile().mkdirs();}// Write to the file bos = new BufferedOutputStream(new FileOutputStream(target));int read = 0;byte[] buffer = new byte[1024 * 10];while ((read = zis.read(buffer, 0, buffer.length)) != -1) {bos.write(buffer, 0, read);}bos.flush();}zis.closeEntry();}catch (IOException e) {throw new RuntimeException(e);} finally {IOUtil.closeQuietly(zis, bos);}}} public static void main(String[] args) {String targetPath = "E://Win7 wallpaper";File file = ZipUtil.zip(targetPath);System.out.println(file);ZipUtil.unzip("F://Win7 wallpaper.zip");}}The following is a Java language description for closing one or more stream objects through the IO stream tool class to obtain a list of closed stream objects, as follows:
package com.utility.io;import java.io.Closeable;import java.io.IOException;/** * IO stream tool class* * @author liujiduo * */public class IOUtil {/** * Close one or more stream objects* * @param closeables * List of closed stream objects* @throws IOException */public static void close(Closeable... closeables) throws IOException {if (closeables != null) {for (Closeable closeable : closeables) {if (closeable != null) {closeable.close();}}}}}/** * Close one or more stream objects* * @param closeables * List of stream objects that can be closed*/public static void closeQuietly(Closeable... closeables) {try {close(closeables);}catch (IOException e) {// do nothing}}}Summarize
The above is the entire content of this article about the code example of the Java compressed file tool ZipUtil usage method, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out.