从压缩文件流中读取数据

public class ZipUtils {
	
	@Test
	public void getZipFile() throws IOException {
        String generateUrl = "http://文件地址";
		//从服务器请求文件流,具体代码就不贴了
//        CloseableHttpResponse response = HttpSender.toPost(FILE_URL, null);
//        InputStream inputStream = response.getEntity().getContent();
		URL url = new URL(generateUrl);
		URLConnection conn = url.openConnection();
		InputStream inputStream = conn.getInputStream();
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        byte[] buff = new byte[1024*1024]; //如果是稍微大的文件,这里配置的大一些
        int len = 0;
        while((len = inputStream.read(buff)) > 0) {
            //把从服务端读取的文件流保存到ByteArrayOutputSteam中
            byteArray.write(buff, 0, len);
            byteArray.flush();
        }
        inputStream.close();
//        response.close();

        //GZIPInputstream解压文件,然后读取文件
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(new GZIPInputStream(
                        new ByteArrayInputStream(byteArray.toByteArray())), "utf-8"));
        String line = null;
        while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }
	
}

参考:https://www.cnblogs.com/jianyong-long/p/9693061.html

你可能感兴趣的:(java基础,java,io文件流,zip)