Java—将文件压缩为zip文件

摘要:
=-1){zos.write(bufs,0,read);{zos.write}catch(FileNotFoundExceptione){e.printStackTrace();thrownewRuntimeException(e);}catch(IOExceptione){e.printStackTrace();thrownewRuntimeException(e);}最后{//关闭流尝试{if(null!=bis)bis.Close();if(null)!
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 hwt 
 * 
 */  
public class FileToZip {  
  
    private FileToZip(){}  
    
    /** 
     * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下 
     * @param sourceFilePath :待压缩的文件路径 
     * @param zipFilePath :压缩后存放路径 
     * @param fileName :压缩后文件的名称 
     * @return 
     */  
    public static String fileToZip(String sourceFilePath,String zipFilePath,String fileName){  
        File sourceFile = new File(sourceFilePath);  
        FileInputStream fis = null;  
        BufferedInputStream bis = null;  
        FileOutputStream fos = null;  
        ZipOutputStream zos = null;  
        //判断源文件是否存在  
        if(!sourceFile.exists()){  
            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); } } } } } 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 fileName + ".zip"; } public static void main(String[] args){ String sourceFilePath = "D:\source"; String zipFilePath = "D:\targetzip"; String fileName = "test"; String fn = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName); System.out.println(fn); } }

免责声明:文章转载自《Java—将文件压缩为zip文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇React基础本人学习Makefile的总结和分享下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

利用7z实现一键解压

目的: 实现双击zip,7z,rar等文件时自动解压,解压完毕后自动打开文件夹。 工具: [本文末尾附有所有工具的下载地址] 7z.exe types 步骤: 新建"7z-自动解压"文件夹 把7z.exe放进去 (7z.exe是7zip的命令版本) 保存以下代码为7z-AutoUnpack.bat 7z-AutoUnpack.bat @echo o...

winzip-dos

http://blog.chinaunix.net/uid-74941-id-155436.html 原文 使用winzip命令行对文件打包压缩 大家都知道winzip对文件解压和压缩都易如反掌,但是如何通过程序和命令行对其调用呢?去http://www.winzip.com/downcl.htm 下载dos版的winzip,下载后直接安装,就会在winz...

Linux权限问题(2)-unzip引发的权限问题

背景:依然是上一个朋友,在用php调用unzip命令时,再次出现了权限被拒绝的问题. Notice:此处描述的问题,为使用php命令行执行php文件,因此进程属主为登录的用户,而不是nginx用户. 以root身份登录,给myuser的家目录设置读和执行的权限 Notice:cd到一个目录,需要对该目录具有执行权限,否则会出现权限被拒绝的提示. # chm...

Linux /python --- zipinfo命令

Linux zipinfo命令用于列出压缩文件信息。 执行zipinfo指令可得知zip压缩文件的详细信息。 zipinfo [-12hlmMstTvz][压缩文件][文件...][-x <范本样式>] 参数: -1 只列出文件名称。 -2 此参数的效果和指定"-1"参数类似,但可搭配"-h","-t"和"-z"参数使用。 -h 只列出压缩文...

压缩文件为zip包导致的中文名称的文件变成乱码

借鉴 http://blog.csdn.net/zhifeiyu2008/article/details/15758653 和 http://pennyfeng.blog.163.com/blog/static/3766859820099304299942/ 这两篇文章,一开始导入的是java.util.zip.ZipOutputStream; java....

出现这个错误说明是程序在调用'ZipArchive' 这个类的时候没有成功

出现这个错误说明是程序在调用'ZipArchive' 这个类的时候没有成功,原因是由于在安装php的时候没有增加php zip的支持(非zlib)。在Windows下的解决办法是:1、在php.ini文件中,将extension=php_zip.dll前面的分号“;”去除;(如果没有,请添加extension=php_zip.dll此行并确保php_zip...