---
title: "Java 输入流 解压 读取"
author: "lippeng"
date: "2011-04-11T17:20:15Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-04-11T17:37:19Z"
id: 998084
url: "https://lippeng.iteye.com/blog/998084"
---

一个文件，本来想从Server Down到本地来解析。但本地路径名中，可能会含有中文，而这里又不想写死。在找解决方法时，想到，Down的过程中，本身用的就是流，为什么不直接把流解压，然后解析呢？

 

所以，就有了下面的故事：

 

 

```
package com.lippeng.helloworld;

import java.io.IOException;
import java.net.URL;
import java.util.zip.ZipInputStream;

public class HelloWorld {

    public static void main(String[] args) {

	String path = "http://localhost:9090/dir/file.zip";

	path = path.replaceAll("\\\\", "/");// 如果端口号后面的路径中有\就需要转换了，在这里是废话一句

	ZipInputStream inputStream = null;
	try {
	    URL url = new URL(path);

	    inputStream = new ZipInputStream(url.openStream());// 通过URL拿到输入流，并转换成Zip输入流。装饰就是好啊。。。

	    inputStream.getNextEntry();// 这一句很重要。本例中，压缩包中只有一个文件。如果有多个，要多次执行这句话的。

	    byte[] b = new byte[1];
	    int count = inputStream.read(b);
	    while (count != -1) {
		// 在这里做想做的事情
		System.out.println(b);

		count = inputStream.read(b);
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    try {
		if (inputStream != null) {
		    inputStream.close();
		}
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
    }
}
```