JAVA实现把指定文件夹下的所有文件压缩成zip包
2017-05-01 10:57
826 查看
JAVA实现把指定文件夹下的所有文件压缩成zip包
<div class="article_manage clearfix"> <div class="article_r"> <span class="link_postdate">2014-03-05 15:46</span> <span class="link_view" title="阅读次数">26657人阅读</span> <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(13)</span> <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('JAVA%e5%ae%9e%e7%8e%b0%e6%8a%8a%e6%8c%87%e5%ae%9a%e6%96%87%e4%bb%b6%e5%a4%b9%e4%b8%8b%e7%9a%84%e6%89%80%e6%9c%89%e6%96%87%e4%bb%b6%e5%8e%8b%e7%bc%a9%e6%88%90zip%e5%8c%85','20550537');return false;" title="收藏" target="_blank">收藏</a></span> <span class="link_report"> <a href="#report" onclick="javascript:report(20550537,2);return false;" title="举报">举报</a></span> </div> </div> <div class="embody" style="display:none" id="embody"> <span class="embody_t">本文章已收录于:</span> <div class="embody_c" id="lib" value="{"err":0,"msg":"ok","data":[]}"></div> </div> <style type="text/css"> .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ededed; } .embody_b{ margin:0 ; padding:10px 0; } .embody .embody_t,.embody .embody_c{ display: inline-block; margin-right:10px; } .embody_t{ font-size: 12px; color:#999; } .embody_c{ font-size: 12px; } .embody_c img,.embody_c em{ display: inline-block; vertical-align: middle; } .embody_c img{ width:30px; height:30px; } .embody_c em{ margin: 0 20px 0 10px; color:#333; font-style: normal; } </style> <script type="text/javascript"> $(function () { try { var lib = eval("("+$("#lib").attr("value")+")"); var html = ""; if (lib.err == 0) { $.each(lib.data, function (i) { var obj = lib.data[i]; //html += '<img src="' + obj.logo + '"/>' + obj.name + " "; html += ' <a href="' + obj.url + '" target="_blank">'; html += ' <img src="' + obj.logo + '">'; html += ' <em><b>' + obj.name + '</b></em>'; html += ' </a>'; }); if (html != "") { setTimeout(function () { $("#lib").html(html); $("#embody").show(); }, 100); } } } catch (err) { } }); </script> <div class="category clearfix"> <div class="category_l"> <img src="http://static.blog.csdn.net/images/category_icon.jpg"> <span>分类:</span> </div> <div class="category_r"> <label onclick="GetCategoryArticles('1478041','sxdtzhaoxinguo','top','20550537');"> <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">Java后端技术<em>(80)</em></span> <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;"> <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;"> <div class="subItem"> <div class="subItem_t"><a href="http://blog.csdn.net/sxdtzhaoxinguo/article/category/1478041" target="_blank">作者同类文章</a><i class="J_close">X</i></div> <ul class="subItem_l" id="top_1478041"> </ul> </div> </label> </div> </div> <script type="text/javascript" src="http://static.blog.csdn.net/scripts/category.js"></script> <div class="bog_copyright"> <p class="copyright_p">版权声明:本文为博主原创文章,未经博主允许不得转载。</p> </div>
1.代码如下:
[java] view plain copy print?
<span style=“font-size:18px;background-color: rgb(204, 204, 204);”>package cn.gov.csrc.base.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 将文件夹下面的文件
* 打包成zip压缩文件
*
* @author admin
*
*/
public final class FileToZip {
private FileToZip(){}
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
* @param sourceFilePath :待压缩的文件路径
* @param zipFilePath :压缩后存放路径
* @param fileName :压缩后文件的名称
* @return
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
boolean flag = false;
File sourceFile = new File(sourceFilePath);
11cc0
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if(sourceFile.exists() == false){
System.out.println(”待压缩的文件目录:”+sourceFilePath+“不存在.”);
}else{
try {
File zipFile = new File(zipFilePath + “/” + fileName +“.zip”);
if(zipFile.exists()){
System.out.println(zipFilePath + ”目录下存在名字为:” + fileName +“.zip” +“打包文件.”);
}else{
File[] sourceFiles = sourceFile.listFiles();
if(null == sourceFiles || sourceFiles.length<1){
System.out.println(”待压缩的文件目录:” + sourceFilePath + “里面不存在文件,无需压缩.”);
}else{
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024*10];
for(int i=0;i<sourceFiles.length;i++){
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i]);
bis = new BufferedInputStream(fis, 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
flag = true;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭流
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
return flag;
}
public static void main(String[] args){
String sourceFilePath = ”D:\\TestFile”;
String zipFilePath = ”D:\\tmp”;
String fileName = ”12700153file”;
boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
if(flag){
System.out.println(”文件打包成功!”);
}else{
System.out.println(”文件打包失败!”);
}
}
}
</span>
package cn.gov.csrc.base.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * 将文件夹下面的文件 * 打包成zip压缩文件 * * @author admin * */ public final class FileToZip { private FileToZip(){} /** * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下 * @param sourceFilePath :待压缩的文件路径 * @param zipFilePath :压缩后存放路径 * @param fileName :压缩后文件的名称 * @return */ public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){ boolean flag = false; File sourceFile = new File(sourceFilePath); FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; ZipOutputStream zos = null; if(sourceFile.exists() == false){ System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在."); }else{ try { File zipFile = new File(zipFilePath + "/" + fileName +".zip"); if(zipFile.exists()){ System.out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件."); }else{ File[] sourceFiles = sourceFile.listFiles(); if(null == sourceFiles || sourceFiles.length<1){ System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩."); }else{ fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(new BufferedOutputStream(fos)); byte[] bufs = new byte[1024*10]; for(int i=0;i<sourceFiles.length;i++){ //创建ZIP实体,并添加进压缩包 ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName()); zos.putNextEntry(zipEntry); //读取待压缩的文件并写进压缩包里 fis = new FileInputStream(sourceFiles[i]); bis = new BufferedInputStream(fis, 1024*10); int read = 0; while((read=bis.read(bufs, 0, 1024*10)) != -1){ zos.write(bufs,0,read); } } flag = true; } } } catch (FileNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally{ //关闭流 try { if(null != bis) bis.close(); if(null != zos) zos.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } } return flag; } public static void main(String[] args){ String sourceFilePath = "D:\\TestFile"; String zipFilePath = "D:\\tmp"; String fileName = "12700153file"; boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName); if(flag){ System.out.println("文件打包成功!"); }else{ System.out.println("文件打包失败!"); } } }
2.结果如下:
文件打包成功!
3.到D:/tmp下查看,你会发现生成了一个zip压缩包.
相关文章推荐
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- java web 实现把文件夹下的所有文件压缩成zip并实现下载(原文链接)http://blog.csdn.net/sxdtzhaoxinguo/article/details/20550537)
- java实现检索指定路径下的所有的下属文件夹和文件
- java 把指定文件夹下的所有文件目录和文件都压缩到指定文件夹下
- JAVA---实现检索指定路径下的所有的下属文件夹和文件
- java 递归实现删除或查询指定文件夹下的所有文件
- java实现批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码
- java实现压缩指定文件夹(文件夹包含子文件夹或文件)为zip格式压缩包
- java实现批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码
- java把指定文件夹下的所有文件压缩成zip包
- Java 实现获取指定文件夹下的指定格式的所有文件
- 面试题-程序题-Java递归实现遍历文件目录(所有文件夹和子文件)
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
- 用JAVA扫描指定文件夹下面所有以.txt结尾的文件,并将其绝对路径输出 .