java解压rar以及zip

/**
	 * 解压缩zip包
	 * 
	 * @param zipFilePath zip文件路径
	 * @param targetPath 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下
	 * @throws IOException
	 * @author yayagepei
	 * @date 2008-9-28
	 */
	public String unZip(String zipFilePath, String targetPath) throws IOException {
		OutputStream os = null;
		InputStream is = null;
		ZipFile zipFile = null;
		String resultPath = null;
		try {
			zipFile = new ZipFile(zipFilePath,"GBK");
			String directoryPath = "";
			if (null == targetPath || "".equals(targetPath)) {
				directoryPath = zipFilePath.substring(0, zipFilePath.lastIndexOf("."));
			} else {
				directoryPath = targetPath;
			}
			Enumeration<ZipEntry> entryEnum = zipFile.getEntries();
			if (null != entryEnum) {
				ZipEntry zipEntry = null;
				while (entryEnum.hasMoreElements()) {
					zipEntry = (ZipEntry) entryEnum.nextElement();
					if (zipEntry.isDirectory()) {
						continue;
					}
					if (zipEntry.getSize() > 0) {
						// 文件
						File targetFile = buildFile(directoryPath + File.separator
								+ zipEntry.getName(), false);
						if (zipEntry.getName().contains("webapps")
								&& "index.html".equals(targetFile.getName())) {
							resultPath = targetFile.getAbsolutePath();
						}
						os = new BufferedOutputStream(new FileOutputStream(targetFile));
						is = zipFile.getInputStream(zipEntry);
						byte[] buffer = new byte[4096];
						int readLen = 0;
						while ((readLen = is.read(buffer, 0, 4096)) >= 0) {
							os.write(buffer, 0, readLen);
						}

						os.flush();
						os.close();
					} else {
					}
				}
			}
		} catch (IOException ex) {
			throw ex;
		} finally {
			if (null != zipFile) {
				zipFile = null;
			}
			if (null != is) {
				is.close();
			}
			if (null != os) {
				os.close();
			}
		}
		return resultPath;
	}

	/**
	 * 解压rar格式压缩包。 对应的是java-unrar-0.3.jar,但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar
	 */
	public String unrar(String sourceRar, String destDir) throws Exception {
		String resultPath = null;
		Archive a = null;
		FileOutputStream fos = null;
		try {
			a = new Archive(new File(sourceRar));
			if (null == destDir || "".equals(destDir)) {
				destDir = a.getFile().getParentFile().getAbsolutePath();
			} 
			FileHeader fh = a.nextFileHeader();
			while (fh != null) {
				if (!fh.isDirectory()) {
					// 1 根据不同的操作系统拿到相应的 destDirName 和 destFileName
					String compressFileName = "";
					if (fh.isUnicode()) {
						compressFileName = fh.getFileNameW().trim();
					} else {
						compressFileName = fh.getFileNameString().trim();
					}
					String destFileName = "";
					String destDirName = "";
					// 非windows系统
					if (File.separator.equals("/")) {
						destFileName = destDir +File.separator+ compressFileName.replaceAll("\\\\", "/");
						destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));
						// windows系统
					} else {
						destFileName = destDir + compressFileName.replaceAll("/", "\\\\");
						destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));
					}

					// 2创建文件夹
					File dir = new File(destDirName);
					if (!dir.exists() || !dir.isDirectory()) {
						dir.mkdirs();
					}
					File destFile=new File(destFileName);
					// 3解压缩文件
					fos = new FileOutputStream(destFile);
					if ("index.html".equals(destFile.getName())) {
						resultPath = destFileName;
					}
					a.extractFile(fh, fos);
					fos.close();
					fos = null;
				}
				fh = a.nextFileHeader();
			}
			a.close();
			a = null;
		} catch (Exception e) {
			throw e;
		} finally {
			if (fos != null) {
				try {
					fos.close();
					fos = null;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (a != null) {
				try {
					a.close();
					a = null;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return resultPath;
	}

	/**
	 * 生产文件 如果文件所在路径不存在则生成路径
	 * 
	 * @param fileName 文件名 带路径
	 * @param isDirectory 是否为路径
	 * @return
	 * @author yayagepei
	 * @date 2008-8-27
	 */
	public File buildFile(String fileName, boolean isDirectory) {
		File target = new File(fileName);
		if (isDirectory) {
			target.mkdirs();
		} else {
			if (!target.getParentFile().exists()) {
				target.getParentFile().mkdirs();
				if (target.exists()) {
					target.delete();
				}
				target = new File(target.getAbsolutePath());
			}
		}
		return target;
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java提供了ZipInputStream和ZipOutputStream类来实现对zip文件的解压缩和压缩,同时也提供了解压RAR文件的开源库,如junrar等。以下是一个示例代码,演示如何使用ZipInputStream和ZipEntry来解压zip文件: ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Unzip { public static void main(String[] args) throws IOException { String zipFilePath = "path/to/zip/file.zip"; String destDirectory = "path/to/destination/folder"; unzip(zipFilePath, destDirectory); } public static void unzip(String zipFilePath, String destDirectory) throws IOException { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry = zipIn.getNextEntry(); while (entry != null) { String filePath = destDirectory + File.separator + entry.getName(); if (!entry.isDirectory()) { extractFile(zipIn, filePath); } else { File dir = new File(filePath); dir.mkdir(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); } private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[4096]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); } } ``` 对于RAR文件的解压缩,可以使用开源库junrar: ```java import com.github.junrar.Archive; import com.github.junrar.exception.RarException; import com.github.junrar.impl.FileVolumeManager; import com.github.junrar.rarfile.FileHeader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Unrar { public static void main(String[] args) throws RarException, IOException { String rarFilePath = "path/to/rar/file.rar"; String destDirectory = "path/to/destination/folder"; unrar(rarFilePath, destDirectory); } public static void unrar(String rarFilePath, String destDirectory) throws RarException, IOException { Archive archive = new Archive(new FileVolumeManager(new File(rarFilePath))); FileHeader fileHeader = archive.nextFileHeader(); while (fileHeader != null) { String filePath = destDirectory + File.separator + fileHeader.getFileNameString().trim(); if (fileHeader.isDirectory()) { File dir = new File(filePath); dir.mkdirs(); } else { FileOutputStream fos = new FileOutputStream(new File(filePath)); archive.extractFile(fileHeader, fos); fos.close(); } fileHeader = archive.nextFileHeader(); } archive.close(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值