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(递归) 压缩文件名 源目录 压缩目录 ...
随机推荐
- 执行插入语句时直接返回插入信息的自增id,判断是否为空
insert into userinfo(UserName,UserPass,RegTime,email)values('a','b',GETDATE(),'123@qq.com');select @ ...
- Android学习1
Activity学习(1) 只有一个Activity 进行Toast通知 Toast是一种短小的提醒,显示一段时间就会消失,试验学习,可以通过一个Button来实现. Button reg=(Butt ...
- net发布mvc项目
1.复制 Web 文件夹 2.复制 DLL 文件C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies C ...
- Vi 几个实用的命令
vi有三种工作模式:指令模式.编辑模式和命令模式. 我们从打开vi说起,这样可以确定下学习环境,也方便学习者实践.打开vi,当前模式即为指令模式,此时可以按a, i, 或o进入编辑模式,或按:(冒号) ...
- POJ 1001 解题报告 高精度大整数乘法模版
题目是POJ1001 Exponentiation 虽然是小数的幂 最终还是转化为大整数的乘法 这道题要考虑的边界情况比较多 做这道题的时候,我分析了 网上的两个解题报告,发现都有错误,说明OJ对于 ...
- response 后刷新页面,点击按钮后,禁用该按钮
一,正常的点击按钮后,将其灰显,全部执行完毕再正常显示. this.btnSave.Attributes.Add("onclick", "if (typeof(Page_ ...
- 【BZOJ 3098】 Hash Killer II
Description 这天天气不错,hzhwcmhf神犇给VFleaKing出了一道题:给你一个长度为N的字符串S,求有多少个不同的长度为L的子串.子串的定义是S[l].S[l + 1].... S ...
- 运行windows系统工具命令
appwiz.cpl 卸载/安装程序 wscui.cpl 操作中心 inetcpl.cpl 查看Internet属性 eventvwr 查看监视消息和疑难解答消息 taskmgr 任 ...
- 2817 Tangent的愤怒 - Wikioi
题目描述 Description 如果机房马上要关门了,或者你急着要和MM约会,请直接跳到第六个自然段. 第二段:本题改编自Usaco Training 4.4.2... 第三段:本题加大了数据强度. ...
- 谈谈怎么实现Oracle数据库分区表
谈谈怎么实现Oracle数据库分区表 数据库的读写分离 SQLSERVER性能监控级别步骤 Oracle索引问题诊断与优化(1)