⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ziputil.java

📁 论坛软件系统亦称电子公告板(BBS)系统
💻 JAVA
字号:
package cn.jsprun.utils;

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.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
	static final int BUFFER = 2048;
	public static void zipFile(File fileToZip, ZipOutputStream zos) throws IOException{
		if (fileToZip.isFile()) {
			String name = fileToZip.getName();
			zos.putNextEntry(new ZipEntry(name));
			FileInputStream fis = new FileInputStream(fileToZip);
			int c;
			while ((c = fis.read()) != -1){
				zos.write(c);
			}
			fis.close();
			zos.closeEntry();
		}
	}
	public static List<String> unZipFile(File zipFile) throws IOException {
		List<String> fileNames = new ArrayList<String>();
		if(!zipFile.exists()){
			throw new IOException("系统找不到指定的压缩文件。");
		}
		String path=zipFile.getPath();
		int index=path.lastIndexOf("\\");
		String folderName=null;
		if(index!=-1){
			folderName =path.substring(0,index);
		}else{
			folderName="";
		}
		BufferedOutputStream dest = null;
		FileInputStream fis = new FileInputStream(zipFile);
		BufferedInputStream bis=new BufferedInputStream(fis);
		ZipInputStream zis = new ZipInputStream(bis);
		ZipEntry entry;
		while ((entry = zis.getNextEntry()) != null) {
			fileNames.add(entry.getName());
			int count;
			byte data[] = new byte[BUFFER];
			FileOutputStream fos = new FileOutputStream(folderName + "\\"+ entry.getName());
			dest = new BufferedOutputStream(fos, BUFFER);
			while ((count = zis.read(data, 0, BUFFER)) != -1) {
				dest.write(data, 0, count);
			}
			dest.flush();
			dest.close();
		}
		zis.close();
		bis.close();
		fis.close();
		return fileNames.size()>0?fileNames:null;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -