JAVA实现文件的压缩与解压

压缩

public class ZipUtilt {
	public static boolean toZip(ServletResponse response, String filePath) {
		try {
			// 获取被压缩文件
			File file = new File(filePath);
			// 从压缩文件中获取输出流
			OutputStream baseOS = response.getOutputStream();
			ZipOutputStream zipOutputStream = new ZipOutputStream(baseOS);
			// 被压缩文件和目录分不同的方式压缩
			boolean isSuccess=fileToZip(file, zipOutputStream, "");
			zipOutputStream.flush();
			zipOutputStream.close();
			baseOS.flush();
			baseOS.close();
			return isSuccess;
			
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	// 主要是针对目录好做递归处理抽取方法
	public static boolean fileToZip(File file, ZipOutputStream zipOutputStream, String zipEntryName) throws Exception {
		if (file.isFile()) {
			// 获取zip实体的名字(zip文件内的文件路径,单个“/”表示压缩包内的顶级目录)
			if(zipEntryName.isEmpty()) {
				zipEntryName = file.getName().replaceAll("\\\\", "/");
			}else {
				zipEntryName = zipEntryName + "/" + file.getName().replaceAll("\\\\", "/");
			}
			// 从被压缩件中获取输入流,读取被压缩文件
			InputStream baseIS = new FileInputStream(file);
			// 向输出流中放入一个zipEntry:zipEntryName就是压缩文件内容的路径,一个zipEntry就是压缩文件中的一个条目
			ZipEntry zipEntry=new ZipEntry(zipEntryName);
			zipOutputStream.putNextEntry(zipEntry);
			byte[] bytes = new byte[1024];
			int lengeth;
			while ((lengeth = baseIS.read(bytes)) != -1) {
				zipOutputStream.write(bytes, 0, lengeth);
				
			}
			// 关闭资源
			baseIS.close();
			zipOutputStream.flush();
			zipOutputStream.closeEntry();
			return true;
		} else {
			// 如果是目录
			if(zipEntryName.isEmpty()) {
				zipEntryName = file.toString().substring(file.toString().replaceAll("\\\\", "/").lastIndexOf("/") + 1);
			}else {
				zipEntryName = zipEntryName + "/"+ file.toString().substring(file.toString().replaceAll("\\\\", "/").lastIndexOf("/") + 1);
			}
			File[] files = file.listFiles();
			// 如果目录中没有文件(空目录)
			if (files == null || files.length == 0) {
				// 需要保留原来的文件结构时,需要对空文件夹进行处理
				if (true) {
					// 如果是空文件夹就按照原来的路径存放
					zipOutputStream.putNextEntry(new ZipEntry(zipEntryName + "/"));
					// 没有文件关闭资源
					zipOutputStream.closeEntry();
				}
			} else {
				zipOutputStream.putNextEntry(new ZipEntry(zipEntryName + "/"));
				for (File contentFile : files) {
					fileToZip(contentFile, zipOutputStream, zipEntryName);
				}
			}
			return true;
		}

	}
}

解压

//解压文件
public class ZipUtilt {
	public static void zipToFile(String zipPath){
		try {
			InputStream fileImputStream = new FileInputStream(zipPath);
			String encoding = System.getProperty("file.encoding");
			ZipInputStream zipInputStream = new ZipInputStream(fileImputStream, Charset.forName(encoding));
			ZipEntry zipentry = null;
			while ((zipentry =zipInputStream.getNextEntry())!=null){
				String filePath = zipPath.substring(0,zipPath.indexOf("."))+"/"+zipentry.getName();
				File file = FileUtils.getFile(filePath);
				if (zipentry.isDirectory()){
					if (!file.exists()){
						file.mkdirs();
					}else {
						return;
					}
				}else {
					if (!file.exists()){
						File parentFile = file.getParentFile();
						if (!parentFile.exists()) {
							parentFile.mkdirs();
						}
						FileOutputStream fileOutputStream = new FileOutputStream(filePath);
						byte[] bytes = new byte[1024];
						int length = -1;
						while ((length=zipInputStream.read(bytes))!=-1){
							fileOutputStream.write(bytes,0,length);
						}
					}else {
						return;
					}
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) throws Exception {
		zipToFile("E:/Project/java是世界上最好的语言.zip");
	}
}