Java zip file utility class ZipUtil USES method code examples

  • 2020-11-26 18:49:38
  • OfStack

The example of this paper USES Zip input and output stream of Java to compress and extract files. The first part of the code can obtain file path and change the compressed file name, etc. The details are 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.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import com.utility.io.IOUtil;
/**
 *  through Java the Zip I/O stream to achieve compression and decompression of files 
 * 
 * @author liujiduo
 * 
 */
public final class ZipUtil {
	private ZipUtil() {
		// empty
	}
	/**
   *  The compressed file 
   * 
   * @param filePath
   *       The file path to be compressed 
   * @return  The compressed file 
   */
	public static File zip(String filePath) {
		File target = null;
		File source = new File(filePath);
		if (source.exists()) {
			//  Compressed file name = The source file name .zip
			String zipName = source.getName() + ".zip";
			target = new File(source.getParent(), zipName);
			if (target.exists()) {
				target.delete();
				//  Delete old files 
			}
			FileOutputStream fos = null;
			ZipOutputStream zos = null;
			try {
				fos = new FileOutputStream(target);
				zos = new ZipOutputStream(new BufferedOutputStream(fos));
				//  Add the corresponding file Entry
				addEntry("/", source, zos);
			}
			catch (IOException e) {
				throw new RuntimeException(e);
			}
			finally {
				IOUtil.closeQuietly(zos, fos);
			}
		}
		return target;
	}
	/**
   *  Scan add file Entry
   * 
   * @param base
   *       The base path 
   * 
   * @param source
   *       The source file 
   * @param zos
   *      Zip File output stream 
   * @throws IOException
   */
	private static void addEntry(String base, File source, ZipOutputStream zos)
	      throws IOException {
		//  Classified by category, such as: /aaa/bbb.txt
		String entry = base + source.getName();
		if (source.isDirectory()) {
			for (File file : source.listFiles()) {
				//  Recursively lists all files in the directory and adds files Entry
				addEntry(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 the files 
   * 
   * @param filePath
   *       Compressed 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 file parent directory 
						target.getParentFile().mkdirs();
					}
					//  Written 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 the Java language description of closing one or more flow objects through IO flow tool class to obtain the list of flow objects that can be closed, specifically as follows:


package com.utility.io;
import java.io.Closeable;
import java.io.IOException;
/**
 * IO Flow tools 
 * 
 * @author liujiduo
 * 
 */
public class IOUtil {
	/**
   *  Shut down 1 Three or more stream objects 
   * 
   * @param closeables
   *       A 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();
				}
			}
		}
	}
	/**
   *  Shut down 1 Three or more stream objects 
   * 
   * @param closeables
   *       A list of closed stream objects 
   */
	public static void closeQuietly(Closeable... closeables) {
		try {
			close(closeables);
		}
		catch (IOException e) {
			// do nothing
		}
	}
}

conclusion

That's it for the Java zip file utility class ZipUtil using method code examples, and I hope you found them helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out.


Related articles: