Java解压zip文件,Java zip压缩

  Java解压zip文件,Java zip压缩

  

目录

压缩成。活力解压。活力

 

  

压缩成.zip

代码如下:

 

  /** * 压缩成ZIP * * @param srcDir压缩文件夹路径* @param out压缩文件输出流* @throws RuntimeException压缩失败会抛出运行时异常*/public static void to zip(String src dir,OutputStream out)抛出运行时异常{ long start=system。当前时间毫秒();ZipOutputStream zos=null试试{ zos=new ZipOutputStream(out);文件源文件=新文件(src dir);compress(sourceFile,zos,sourceFile.getName(),false);长端=系统。当前时间毫秒();System.out.println(压缩完成,耗时:(end-start) ms );} catch(Exception e){抛出新的运行时异常( zip utils的活力错误,e);}最后{如果(zos!=null){ try { zos。close();} catch(io异常e){ e . printstacktrace();} } }}/** * 递归压缩方法* * @param源文件源文件* @param zos zip输出流* @param name压缩后的名称* @param KeepDirStructure是否保留原来的目录结构,true:保留目录结构;* p * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)* @ throws Exception */private static void compress(文件源文件,ZipOutputStream zos,字符串名称,布尔KeepDirStructure)抛出异常{ byte[]buf=新字节[BUFFER _ SIZE];if (sourceFile.isFile()) { //向活力输出流中添加一个活力实体,构造器中名字为活力实体的文件的名字zos.putNextEntry(新zip条目(名称));//复制文件到活力输出流中int lenfile inputstream in=新文件inputstream(源文件);while ((len=in.read(buf))!=-1) { zos.write(buf,0,len);} //完成条目zos。关闭条目();英寸close();} else { File[]list files=源文件。列出文件();if(列表文件==null 列表文件。长度==0){//需要保留原来的文件结构时,需要对空文件夹进行处理if (KeepDirStructure) { //空文件夹的处理zos.putNextEntry(新ZipEntry(名称/));//没有文件,不需要文件的复制zos。关闭条目();} } else { for(File File :列表文件){//判断是否需要保留原来的文件结构if (KeepDirStructure) { //注意:file.getName()前面需要带上父文件夹的名字加一斜杠, //不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了compress(file,zos,name / file.getName(),KeepDirStructure);} else { compress(file,zos,file.getName(),KeepDirStructure);} } } }}测试验证代码:

  /** * 测试打包本地的Navicat,输出为活力文件* @抛出异常*/@ test public void test()抛出异常{ //Navicat路径string inDir= e : developer Navicat ;//打包后输出路径string out dir= e : developer navicat zip navicat。zip ;输出流文件输出流=新文件输出流(新文件(outDir));ZipUtils.toZip(inDir,文件输出流);}打包前后的文件如下:

  

解压.zip

代码如下:

 

  /** * 解压活力文件到指定目录* @ param file zip * @ param path _ to _ dest * @ throws io exception */public static void readZip(String file zip,String path _ to _ dest)throws io exception { try(file inputstream fis=new file inputstream(file zip);zip inputstream zis=new zip inputstream(new BufferedInputStream(fis))){ zip entry entry;//从ZipInputStream读取每个条目,直到没有//发现更多条目,返回值为空//getNextEntry()方法.while ((entry=zis.getNextEntry())!=null){ system。出去。println( unzip : 条目。getname());(同Internationalorganizations)国际组织大小;字节[]缓冲区=新字节[2048];File File out=new File(path _ to _ dest 条目。getname());try(文件输出流fos=新文件输出流(file out);BufferedOutputStream Bos=new BufferedOutputStream(fos,buffer . length)){ while((size=zis。读取(buffer,0,buffer。长度))!=-1) { bos.write(buffer,0,size);} Bos。flush();} } } catch(io异常e){ e . printstacktrace();}}测试验证代码:

  /** * 测试解压本地活力文件* @ throws异常*/@ test public void readZip()抛出异常{ //解压后路径string path _ to _ dest= e : developer NavicatUnzip ;//zip文件路径字符串文件zip= e : developer navicat zip navicat。zip ;ZipUtils.readZip(fileZip,path _ to _ dest);}解压前后的文件如下:

  以上就是爪哇实现文件压缩为活力和解压活力压缩包的详细内容,更多关于爪哇文件压缩为活力的资料请关注盛行信息技术其它相关文章!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: