1、文件ZIp的对byte[]的压缩和解压缩
2015-07-22 20:42
381 查看
最近在做一个邮件系统,因为要和原来系统兼容,原系统是吧上传的附件进行zip压缩,然后按照32k分段存储,查询了API和相关资料,整理出文件的zip的压缩和解压缩方法,代码如下:
首先:
1、将文件转化为byte[]数组
private byte[] getBytesFromFile(File file) throws IOException {
InputStream in = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
throw new LogicException("文件过大,不能传输");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("不能转换,");
}
in.close();
return bytes;
}
a) 压缩:
public byte[] zip(byte[] data) {
byte[] b = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(bos);
ZipEntry entry = new ZipEntry("~~~1.bmp");
entry.setSize(data.length);//返回条目数据的未压缩大小;如果未知,则返回 -1。
zip.putNextEntry(entry);// 开始写入新的 ZIP 文件条目并将流定位到条目数据的开始处
zip.write(data);//将字节数组写入当前 ZIP 条目数据。
zip.closeEntry();
zip.close();
b = bos.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
}
b) 解压缩:
public byte[] unZip(byte[] data) {
byte[] b = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ZipInputStream zip = new ZipInputStream(bis
c135
);
while (zip.getNextEntry() != null) {
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = zip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
}
zip.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
}
首先:
1、将文件转化为byte[]数组
private byte[] getBytesFromFile(File file) throws IOException {
InputStream in = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
throw new LogicException("文件过大,不能传输");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("不能转换,");
}
in.close();
return bytes;
}
a) 压缩:
public byte[] zip(byte[] data) {
byte[] b = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(bos);
ZipEntry entry = new ZipEntry("~~~1.bmp");
entry.setSize(data.length);//返回条目数据的未压缩大小;如果未知,则返回 -1。
zip.putNextEntry(entry);// 开始写入新的 ZIP 文件条目并将流定位到条目数据的开始处
zip.write(data);//将字节数组写入当前 ZIP 条目数据。
zip.closeEntry();
zip.close();
b = bos.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
}
b) 解压缩:
public byte[] unZip(byte[] data) {
byte[] b = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ZipInputStream zip = new ZipInputStream(bis
c135
);
while (zip.getNextEntry() != null) {
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = zip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
}
zip.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
}
相关文章推荐
- Python中zip()函数用法举例
- zip 的压缩原理与实现
- C#使用DeflateStream解压缩数据文件的方法
- 用vbs实现zip功能的脚本
- C#调用WinRar执行rar、zip压缩的方法
- C#实现页面GZip或Deflate压缩的方法
- C#使用iCSharpcode进行文件压缩实现方法
- C#实现压缩HTML代码的方法
- Asp.net在线备份、压缩和修复Access数据库示例代码
- 使用UglifyJS合并/压缩JavaScript的方法
- 高性能WEB开发 JS、CSS的合并、压缩、缓存管理
- 脚本分析、压缩、混淆工具 JSA新版本发布,压缩效率提高大约10%
- 发布一个高效的JavaScript分析、压缩工具 JavaScript Analyser
- PHP实现图片压缩的两则实例
- 在IIS上启用Gzip压缩 (HTTP压缩)第1/3页
- PHP执行zip与rar解压缩方法实现代码
- javascript实现的样式表(CSS) 格式整理与压缩
- php zlib压缩和解压缩swf文件的代码
- YUI Compressor压缩JavaScript原理及微优化
- Nginx中的Gzip压缩配置介绍