栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

		
		
			org.apache.ant
			ant
			1.9.7
		
		
		
			commons-io
			commons-io
			2.4
		

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

	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());
	}

	
	
	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文件,返回访问路径
另一个是解压读取里面的内容

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/886133.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号