志在指尖
用双手敲打未来

ZIP文件压缩

在开发过程中遇到一个小需求,将附件中的文件停止紧缩,紧缩文件以前没怎样接触过,记载下,直接上代码:
/***
*将附件中的文件紧缩
*
*@paramfileMap
紧缩前的文件
*@returnMap<String,byte[]>
紧缩后的文件java
*/
privateMap<String,byte[]>filesCompressionZIP(Map<String,byte[]>fileMap){
Map<String,byte[]>map=newHashMap();
ByteArrayOutputStreambos=newByteArrayOutputStream();
ZipOutputStreamzip=newZipOutputStream(bos);
try{
ZipEntryentry=null;
for(Map.Entry<String,byte[]>en:fileMap.entrySet()){
entry=newZipEntry(en.getKey());
entry.setSize(en.getValue().length);
zip.putNextEntry(entry);
zip.write(en.getValue());
}
}catch(Excepritone){
LOGGER.error(e.getMessage(),e);
}finally{
try{
zip.closeEntry();
}catch(IOExcepritone){
LOGGER.error(e.getMessage(),e);
}
try{
zip.close();
}catch(IOExcepritone){
LOGGER.error(e.getMessage(),e);
}
try{
bos.close();
}catch(IOExcepritone){
LOGGER.error(e.getMessage(),e);
}
}
map.put(System.currentTimeMillis()+”.zip”,bos.toByteArray());
returnmap;
}
看代码表现你的价值

未经允许不得转载:IT技术网站 » ZIP文件压缩
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载