zip文件解压或压缩
- <span style="font-size:18px;">/**
- * lsz
- */
- public final class ZipUtil {
- /**
- * 解压zip文件
- * @param unZipfile
- * @param destFile
- */
- public static void unZip(String unZipfile, String destFile) {
- FileOutputStream fileOut;
- File file;
- InputStream inputStream;
- byte[] buf = new byte[1024*4];
- try {
- //生成一个zip的文件
- ZipFile zipFile = new ZipFile(unZipfile, "GBK");
- //遍历zipFile中所有的实体,并把他们解压出来
- for (@SuppressWarnings("unchecked")
- Enumeration<ZipEntry> entries = zipFile.getEntries(); entries
- .hasMoreElements();) {
- ZipEntry entry = entries.nextElement();
- //生成他们解压后的一个文件
- file = new File(destFile+File.separator+entry.getName());
- if (entry.isDirectory()) {
- file.mkdirs();
- } else {
- // 如果指定文件的目录不存在,则创建之.
- File parent = file.getParentFile();
- if (!parent.exists()) {
- parent.mkdirs();
- }
- //获取出该压缩实体的输入流
- inputStream = zipFile.getInputStream(entry);
- fileOut = new FileOutputStream(file);
- int length = 0;
- //将实体写到本地文件中去
- while ((length = inputStream.read(buf)) > 0) {
- fileOut.write(buf, 0, length);
- }
- fileOut.close();
- inputStream.close();
- }
- }
- zipFile.close();
- //解压完后将原压缩文件删除
- File zipfile = new File(unZipfile);
- if(zipfile.exists()){
- zipfile.delete();
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- /**
- * 一个文件夹压缩
- * 压缩文件夹
- * @param filepath
- * @param savepath
- * @throws Exception
- */
- public static void toZip(String filepath,String savepath) throws Exception{
- File file = new File(filepath);
- if(file.exists()){
- //判断导出路径是否为空,如果为空,则将压缩包生成到当前路径下
- if(StringUtils.isBlank(savepath)){
- savepath = filepath+".zip";
- }else{
- savepath = savepath+".zip";
- }
- ZipOutputStream outPut = new ZipOutputStream(new FileOutputStream(new File(savepath)));
- outPut.setEncoding("GBK");//设置编码
- createZip(outPut,file.listFiles(),null);
- outPut.flush();
- outPut.close();
- }else{
- //not found
- throw new RuntimeException("Err :not found file exception:"+filepath);
- }
- }
- private static void createZip(org.apache.tools.zip.ZipOutputStream outPut,File[] listFiles,String fuPath) throws Exception {
- for(File f : listFiles){
- String name = fuPath==null?f.getName():fuPath+"/"+f.getName();;
- if(f.isDirectory()){
- outPut.putNextEntry(new ZipEntry(name+"/"));
- createZip(outPut,f.listFiles(),name);
- }else{
- outPut.putNextEntry(new ZipEntry(name));
- InputStream is = new FileInputStream(f);
- byte[] bys = new byte[1024];
- int len = 0;
- while((len = is.read(bys))!=-1)
- outPut.write(bys, 0, len);
- is.close();
- outPut.flush();
- }
- }
- }
- /*
- * 复制文件 只能使复制文件,不能复制文件夹
- */
- public static void fileChannelCopy(File fromfile, File tofile) {
- FileInputStream fi = null;
- FileOutputStream fo = null;
- FileChannel in = null;
- FileChannel out = null;
- try {
- fi = new FileInputStream(fromfile);
- fo = new FileOutputStream(tofile);
- in = fi.getChannel();//得到对应的文件通道
- out = fo.getChannel();//得到对应的文件通道
- in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- fi.close();
- in.close();
- fo.close();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }</span>
zip文件解压或压缩的更多相关文章
- c++builder ZIP文件解压与压缩(ZLIB DLL调用)(转载 )
转载:http://blog.csdn.net/goodai007/article/details/7414512 头文件:ZipAndFile.h //----------------------- ...
- ZIP文件解压
public class DZip { /// <summary> /// 压缩为ZIP文件 /// </summary> public void Zip(string dir ...
- linux下压缩成zip文件解压zip文件
linux zip命令的基本用法是: zip [参数] [打包后的文件名] [打包的目录路径] linux zip命令参数列表: -a 将文件转成ASCII模式 -F 尝试修复损坏 ...
- Linux:文件解压与压缩
文件打包与压缩 常见压缩文件格式: |文件后缀名 |说明| |.zip |zip程序打包压缩的文件| |.rar |rar程序压缩的文件| |.7z |7zip程序压缩的文件| |.tar |tar程 ...
- asp.net实现文件解压和压缩
C#解压RAR压缩文件(--转载--测试通过) using System; using System.Collections.Generic; using System.Text; using Sys ...
- Linux 下 zip 文件解压乱码解决方案,ubuntu16.10亲测可用
文章来源: https://www.zhihu.com/question/20523036 今天邮件中收到了一个压缩文件,解压后却是乱码,从网上也找了几个方法,目前这个方法还是比较可靠的,如下所示: ...
- Python实现加密的ZIP文件解压(密码已知)
博主在上篇博文介绍了<Python实现加密的RAR文件解压(密码已知)>后,又尝试了ZIP文件的解压方法,下面开始分享. 当ZIP文件的压缩密码已知时,可以通过调用zipfile库进行解压 ...
- linux .tar.xz 文件解压和压缩
场景:centos7.0下文件格式为xxx.tar.xz,解压和压缩命令: 压缩 tar -Jcf linux-3.10.0-123.13.1.el7.tar.xz(文件名) linux-3.10.0 ...
- Liunx文件解压与压缩
文件压缩和解压缩 常见压缩格式如下 .zip .gz .bz2 .tar.gz .tar.gz2 .zip压缩 zip 压缩文件名 源文件 压缩文件 zip -r(递归) 压缩文件名 源目录 压缩目录 ...
随机推荐
- FileStream使用小记
流用于对IO处理 在System.IO名称空间中有以下类 BinaryReader/Writer TextReader/Writer Stream 其中类Stream为抽象类.由此有三个派生类: Me ...
- sql server 查看创建视图的sql语句
--sp_helptext v_viewnamesp_helptext port_dept--效果
- 从 Typecho 自定义字段的调用代码看去
千呼万唤,Typecho 的"自定义字段"功能终于在 0.9 中出来了.然而,多数人还蒙在这样一个鼓里--该怎么在模板调用已经设置好的自定义字段呢?让我们从这里开始说下去: Typ ...
- 《C和指针》 读书笔记 -- 第13章 高级指针话题
1.函数指针 int (*f)(); int *(*f[])(); 用途: [1]回调函数 e.g. /*在一个单链表中查找指定值*/ Node *search_list(Node *node,voi ...
- C/C++错误分析errno,perror,strerror和GetLastError()函数返回的错误代码的意义
在C语言编译中,经常会出现一些系统的错误,这些错误如果在编译的时候不能很好的“预见”,会使系统“崩溃”,常见的捕获错误函数有: errno #include<errno.h> 这个变量是程 ...
- poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串
题目链接 题意:输入N(2 <= N <= 4000)个长度不超过200的字符串,输出字典序最小的最长公共连续子串; 思路:将所有的字符串中间加上分隔符,注:分隔符只需要和输入的字符不同, ...
- TextBox控件
1.通过设置Multiline属性(bool)来控制文本框是否为多行显示 txt_Change.Location = , );//设置文本框位置 txt_Change.Multiline = true ...
- C#中用ILMerge将所有引用的DLL打成一个DLL文件
有些文件是必须一起使用的,如果能把多个DLL打包成一个DLL文件,那么引用文件的时候就不需要一个个地去引用,而且每次移动文件的时候也不至于少了哪个必须的DLL文件. 多个DLL文件打包成一个DLL文件 ...
- MySQL 主主同步配置和主从配置步骤
★预备知识 : 1.双机热备 对于双机热备这一概念,我搜索了很多资料,最后,还是按照大多数资料所讲分成广义与狭义两种意义来说. 从广义上讲,就是对于重要的服务,使用两台服务器,互相备份,共同执行同一服 ...
- [转载]Eziriz .NET Reactor 4.7.0.0 官方原版+破解补丁(强大的代码保护和软件防盗版工具)
Eziriz .NET Reactor 是一个强大的代码保护和软件防盗版工具,完全由.NET框架编写..NET Reactor支持NET平台的软件许可系统,并支持NET程序集所有语言.当.Net编译器 ...