java 字符串生成zip文件和解读zip

Java 实现生成外部文件zip 和 解压zip
话不多说,先上jar

		<!-- ant -->
		<dependency>
			<groupId>org.apache.ant</groupId>
			<artifactId>ant</artifactId>
			<version>1.9.7</version>
		</dependency>
		
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>

我这用的是pom.xml的配置,不是的可以直接下载2个jar放入到项目中
下面直接上代码

/**
	 * 生成zip文件
	 * Title: saveToDescription  
	 * Description: 
	 * @author Zhangjl
	 * @param Description
	 * @param UID
	 * @param shop
	 * @param itemid
	 * @return
	 */
	public static String saveToDescription(String Description, String UID, String shop, String itemid) {

		//本地地址
		String startpath = "c:\\description\\";
		//网络访问地址
		String ipPath = "http://localhost:8080/description";

		File f = null;
		String fileName = "";
		String path = startpath + File.separator + UID + File.separator + shop;
		path = path.replace("/", "_").replace(" ", "_");
		fileName = itemid;
		f = new File(path);
		if (!f.exists()) {
			if (!f.mkdirs()) {
				System.out.println("不能创建目录:" + path);
				return "";
			}
		}

		File file = new File(path, fileName + ".zip");
		try (ZipOutputStream zos = new ZipOutputStream(file)) {
			zos.putNextEntry(new ZipEntry(fileName + ".txt"));
			zos.write(Description.getBytes(Charsets.UTF_8));
			zos.closeEntry();
			zos.flush();

			String url = file.getAbsolutePath();
			url = url.replace(startpath, ipPath).replace("\\", "/");
			return url;
		} catch (IOException e) {
			e.printStackTrace();
		}

		return "";

	}

	public static boolean isNullOrEmpty(Object obj) {
		return obj == null || "".equals(obj.toString());
	}

	
	/**
	 * 解压读取zip的内容
	 * Title: readDescribeFromFile  
	 * Description: 
	 * @author Zhangjl
	 * @param descriptionURL
	 * @return
	 */
	public static String readDescribeFromFile(String descriptionURL) {

		if (!isNullOrEmpty(descriptionURL)) {
			String tempFile = "";

			if (descriptionURL.contains("向易"))
				descriptionURL = descriptionURL.replace("向易", "%E5%90%91%E6%98%93");
			// 读取描述文件的代码
			try {
				if (!isNullOrEmpty(descriptionURL)) {
					URL url = new URL(descriptionURL);
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					conn.setConnectTimeout(3 * 1000);
					// 得到输入流
					InputStream inputStream = conn.getInputStream();
					ZipInputStream zipInputStream = new ZipInputStream(inputStream);
					BufferedInputStream bs = new BufferedInputStream(zipInputStream);
					java.util.zip.ZipEntry nextEntry;
					nextEntry = zipInputStream.getNextEntry();
					while (nextEntry != null) {
						tempFile = nextEntry.getName();
						File file = new File(tempFile);
						// 如果是目录,创建目录
						if (tempFile.endsWith("/")) {
							file.mkdir();
						} else {
							// 文件则写入具体的路径中
							FileOutputStream fileOutputStream = new FileOutputStream(file);
							BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
							int n;
							byte[] bytes = new byte[1024];
							while ((n = zipInputStream.read(bytes)) != -1) {
								bufferedOutputStream.write(bytes, 0, n);
							}
							// 关闭流
							bufferedOutputStream.close();
							fileOutputStream.close();
						}
						// 关闭当前布姆
						zipInputStream.closeEntry();
						// 读取下一个目录,作为循环条件
						nextEntry = zipInputStream.getNextEntry();
					}
					String returnContent = readFile(tempFile);
					deleteFile(tempFile);
					return returnContent;

				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "";

	}

	public static String readFile(String fileName) {
		// custName = new String(custName.getBytes("ISO-8859-1"), "UTF-8");
		String fileContent = "";
		try {
			File f = new File(fileName);
			if (f.isFile() && f.exists()) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(f), "UTF-8");
				BufferedReader reader = new BufferedReader(read);
				String line;
				while ((line = reader.readLine()) != null) {
					fileContent += line;
					fileContent += "\\n";
				}
				read.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileContent;
	}

	public static boolean deleteFile(String sPath) {
		Boolean flag = false;
		File file = new File(sPath);
		if (file.isFile() && file.exists()) {
			file.delete();
			flag = true;
		}
		return flag;
	}

这里面一个是压缩并生成zip文件,返回访问路径
另一个是解压读取里面的内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值