zip压缩流转本地文件及解压
2016-01-07 15:55
381 查看
有一个需求,HTTP获取到zip文件的byte数组,需要转为本地的zip或是解压zip文件。
1. 用到的class
2 获取解压后的文件
3. 转存zip文件,(可修改zip内的文件名)
或者简单粗暴的直接将其保存到本地文件
private static void getZipFile(byte[] data) throws Exception {
String filename = "/log/111/111.zip";
File targetFile = new File(filename);
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(data);
outStream.flush();
outStream.close();
}
1. 用到的class
import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import java.io.ByteArrayInputStream;
2 获取解压后的文件
private static void getTxtFile(byte[] data) throws Exception { ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(data)); ZipEntry entry = null; while ((entry = zipStream.getNextEntry()) != null) { String entryName = entry.getName(); FileOutputStream out = new FileOutputStream("/log/111/" + entryName); byte[] byteBuff = new byte[4096]; int bytesRead = 0; while ((bytesRead = zipStream.read(byteBuff)) != -1) { out.write(byteBuff, 0, bytesRead); } out.close(); zipStream.closeEntry(); } zipStream.close(); }
3. 转存zip文件,(可修改zip内的文件名)
private static void getZipFile(byte[] data) throws Exception { String filename = "/log/111/111.zip"; FileOutputStream fileOutputStream = new FileOutputStream(filename); ZipOutputStream zos = new ZipOutputStream(fileOutputStream); ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(data)); ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { ZipEntry entry1 = new ZipEntry(entry.getName()); zos.putNextEntry(entry1); zipStream.closeEntry(); } zos.write(data); zos.flush(); zos.closeEntry(); zos.close(); zipStream.close(); }
或者简单粗暴的直接将其保存到本地文件
private static void getZipFile(byte[] data) throws Exception {
String filename = "/log/111/111.zip";
File targetFile = new File(filename);
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(data);
outStream.flush();
outStream.close();
}
相关文章推荐
- jquery的animate设置样式overflow:hidden的解决方法
- centos6.5部署nfs共享目录
- LeetCode 25 - Reverse Nodes in k-Group
- android中DatePickerDialog只显示年月
- CentOS 6.4 x64 系统回收站功能
- CSS应用
- MySql高级三
- SQL SERVER 清理日志
- 10.UIApplication的基本使用
- [leetcode] 141. Linked List Cycle 解题报告
- 在eclipse中创建 maven-web 项目
- Ubuntu root用户密码设置以及环境变量
- Flask源码阅读(三)——渲染模板
- Maven安装和配置,eclipse创建Maven项目
- ssl
- Uml类图简介
- Integer 和 int 的比较
- 详解WordPress开发中wp_title()函数的用法
- Centos 7 Hadoop-2.5.1 分布式 环境搭建
- IsPostBack详解