溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中如何使用ZipUtil壓縮文件工具類

發(fā)布時(shí)間:2021-07-22 16:53:57 來源:億速云 閱讀:823 作者:Leah 欄目:編程語言

這篇文章給大家介紹Java中如何使用ZipUtil壓縮文件工具類,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

具體如下:

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;
/**
 * 通過Java的Zip輸入輸出流實(shí)現(xiàn)壓縮和解壓文件
 * 
 * @author liujiduo
 * 
 */
public final class ZipUtil {
	private ZipUtil() {
		// empty
	}
	/**
   * 壓縮文件
   * 
   * @param filePath
   *      待壓縮的文件路徑
   * @return 壓縮后的文件
   */
	public static File zip(String filePath) {
		File target = null;
		File source = new File(filePath);
		if (source.exists()) {
			// 壓縮文件名=源文件名.zip
			String zipName = source.getName() + ".zip";
			target = new File(source.getParent(), zipName);
			if (target.exists()) {
				target.delete();
				// 刪除舊的文件
			}
			FileOutputStream fos = null;
			ZipOutputStream zos = null;
			try {
				fos = new FileOutputStream(target);
				zos = new ZipOutputStream(new BufferedOutputStream(fos));
				// 添加對(duì)應(yīng)的文件Entry
				addEntry("/", source, zos);
			}
			catch (IOException e) {
				throw new RuntimeException(e);
			}
			finally {
				IOUtil.closeQuietly(zos, fos);
			}
		}
		return target;
	}
	/**
   * 掃描添加文件Entry
   * 
   * @param base
   *      基路徑
   * 
   * @param source
   *      源文件
   * @param zos
   *      Zip文件輸出流
   * @throws IOException
   */
	private static void addEntry(String base, File source, ZipOutputStream zos)
	      throws IOException {
		// 按目錄分級(jí),形如:/aaa/bbb.txt
		String entry = base + source.getName();
		if (source.isDirectory()) {
			for (File file : source.listFiles()) {
				// 遞歸列出目錄下的所有文件,添加文件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);
			}
		}
	}
	/**
   * 解壓文件
   * 
   * @param filePath
   *      壓縮文件路徑
   */
	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()) {
						// 創(chuàng)建文件父目錄
						target.getParentFile().mkdirs();
					}
					// 寫入文件
					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壁紙";
		File file = ZipUtil.zip(targetPath);
		System.out.println(file);
		ZipUtil.unzip("F:\\Win7壁紙.zip");
	}
}

下面是通過IO流工具類實(shí)現(xiàn)關(guān)閉一個(gè)或多個(gè)流對(duì)象的Java語言描述,獲取可關(guān)閉的流對(duì)象列表,具體如下:

package com.utility.io;
import java.io.Closeable;
import java.io.IOException;
/**
 * IO流工具類
 * 
 * @author liujiduo
 * 
 */
public class IOUtil {
	/**
   * 關(guān)閉一個(gè)或多個(gè)流對(duì)象
   * 
   * @param closeables
   *      可關(guān)閉的流對(duì)象列表
   * @throws IOException
   */
	public static void close(Closeable... closeables) throws IOException {
		if (closeables != null) {
			for (Closeable closeable : closeables) {
				if (closeable != null) {
					closeable.close();
				}
			}
		}
	}
	/**
   * 關(guān)閉一個(gè)或多個(gè)流對(duì)象
   * 
   * @param closeables
   *      可關(guān)閉的流對(duì)象列表
   */
	public static void closeQuietly(Closeable... closeables) {
		try {
			close(closeables);
		}
		catch (IOException e) {
			// do nothing
		}
	}
}

關(guān)于Java中如何使用ZipUtil壓縮文件工具類就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI