Java:数据的压缩和解压,及数据的网络传输。

1.批量压缩数据

public static byte[] zip(Map<String, byte[]> dataMap) {

	byte[] b = null;
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	ZipOutputStream zip = new ZipOutputStream(bos);

	try {
		Set<Entry<String, byte[]>> entrySet = dataMap.entrySet();
		for (Entry<String, byte[]> entry : entrySet) {
			ZipEntry cacheEntry = new ZipEntry(entry.getKey());
			cacheEntry.setSize(entry.getValue().length);
			zip.putNextEntry(cacheEntry);
			zip.write(entry.getValue());
			zip.closeEntry();
		}
		zip.close();
		b = bos.toByteArray();
		bos.close();
	} catch (Exception ex) {
		ex.printStackTrace();
	}
	return b;
}

方法传入的参数是Map,该Map的键:压缩文件的名称,Map的值:压缩文件的内容。

每个压缩文件对应一个ZipEntry。

结果返回一个byte[]。

2.将压缩文件存储到文件夹中

	private static void saveZip(byte[] by) throws Exception{
		// 将压缩文件的内容,存储到如下路径:D:\\zjjZipTest
		OutputStream out = new FileOutputStream("D:\\zjjZipTest");
		InputStream is = new ByteArrayInputStream(by);
		byte[] buff = new byte[1024];
		int len = 0;
		while ((len = is.read(buff)) != -1) {
			out.write(buff, 0, len);
		}
		is.close();
		out.close();
	}

3.解压缩指定文件夹的文件,并存储到新的文件夹中

	private static void zjjUnzip() throws Exception {
		// 将被解压缩的文件对象
		File file = new File("D:\\zjjZipTest");

		// get a ZipFile instance
		ZipFile zipFile = new ZipFile(file);

		// create a ZipInputStream instance
		ZipInputStream zis = new ZipInputStream(new FileInputStream(file));

		// create a ZipEntry instance , lay the every file from
		// decompress file temporarily
		ZipEntry entry = null;

		// a circle to get every file
		while ((entry = zis.getNextEntry()) != null) {
			System.out.println("decompress file :" + entry.getName());

			// 解压之后的内容输出到此路径下
			File outFile = new File("D:\\zjjZipTestOut\\" + entry.getName());

			// if the file's parent directory wasn't exits ,than
			// create the directory
			if (!outFile.getParentFile().exists()) {
				outFile.getParentFile().mkdir();
			}

			// if the file not exits ,than create the file
			if (!outFile.exists()) {
				outFile.createNewFile();
			}

			// create an input stream
			BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

			// create an output stream
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));
			byte[] b = new byte[100];
			while (true) {
				int len = bis.read(b);
				if (len == -1)
					break;
				bos.write(b, 0, len);
			}
			// close stream
			bis.close();
			bos.close();
		}
		zis.close();
	}

4.读取解压缩文件夹中的文件,得到文件内容

	//读取解压缩文件中的内容
	private static byte[] readUnzipData() throws Exception {

		//从此文件中读取内容
		File file = new File("D:\\zjjZipTestOut\\newestName");
		// File file = new File("D:\\zjjZipTestOut\\cacheName");
		FileInputStream in = new FileInputStream(file);

		// size 为字串的长度 ,这里一次性读完
		int size = in.available();
		byte[] buffer = new byte[size];
		in.read(buffer);

		in.close();
		return buffer;
	}

5.代码流程

	//流程
	public static void main(String[] args) throws Exception {
		// 创建数据 名称-urls map
		Map<String, byte[]> dataMap = new HashMap<>();
		dataMap.put("cacheName", makeJson("cacheUrlPath"));
		dataMap.put("newestName", makeJson("newrestUrlPath"));

		// 压缩此map
		byte[] by = zip(dataMap);

		// 将压缩文件存储文件夹
		saveZip(by);

		// 解压缩,并把解压缩后的文件存储到zjjZipTestOut文件夹中,压缩文件名是map的键
		zjjUnzip();

		// 读取解压缩后文件中的内容,并以json格式输出
		byte[] by2 = readUnzipData();
		
		// 输出结果
		String s = new String(by2);
		System.out.println(s);
		JSONObject j = new JSONObject(s);
		System.out.println(j);
		System.out.println(j.get("url"));
	}

	private static byte[] makeJson(String content) {
		JSONObject j = new JSONObject();
		j.put("url", content);
		return j.toString().getBytes();
	}

文件生成结果

zjjZipTest:压缩文件

zjjZipTestOut:解压后的文件的输出路径,包含cacheName和newestName。

输出结果

总结

数据的压缩和解压在网络传输中有着重要地位。

猜你喜欢

转载自blog.csdn.net/qq_32293345/article/details/81287380