注意这里用的是apche下的zip

package org.springframework.validation;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import java.io.*;
import java.util.Enumeration; /**
* @author qinlinsen
*/
public class ZipFileUtils {
private static final String ZIP_FILE_NOT_EXISTS = "zip file doesn't exists";
private static final String DEFAULT_ZIP_CHARSET = "GBK";
private static final String UNZIP_OPERATION_SUCCESS = "doUnzip operation success ";
private static final String UNZIP_FILE_PATH_CANNOT_BE_NULL = "unzip file absolute path cannot be null";
private static final String ZIP_FILE_PATH_CANNOT_BE_NULL = "zip file absolute path cannot be null";
private static final String FILE_SEPARATOR = "file.separator"; /**
* 解压操作
* @param zipFilePath zip文件的绝对路径
* @param unzipFilePath 解压之后文件的绝对路径
* @return
*/
public static String doUnzip(String zipFilePath, String unzipFilePath) {
//validate input parameters
Assert.notNull(unzipFilePath, UNZIP_FILE_PATH_CANNOT_BE_NULL);
Assert.notNull(zipFilePath, ZIP_FILE_PATH_CANNOT_BE_NULL);
ZipFile zipFile = null;
String directoryName = "";
try {
zipFile = new ZipFile(zipFilePath, DEFAULT_ZIP_CHARSET);
} catch (IOException e) {
System.out.println(ZIP_FILE_NOT_EXISTS);
e.printStackTrace();
}
Enumeration<ZipEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if (zipEntry.isDirectory()) {
directoryName = zipEntry.getName();
directoryName = directoryName.substring(, directoryName.length() - );
if(!unzipFilePath.endsWith(System.getProperty(FILE_SEPARATOR))){
unzipFilePath=unzipFilePath+System.getProperty(FILE_SEPARATOR);
}
File folder = new File(unzipFilePath + directoryName); //create a folder
folder.mkdir();
} else {
InputStream in = null;
OutputStream out = null;
try {
File file = new File(unzipFilePath + zipEntry.getName());
//create a parent file
file.getParentFile().mkdir();
//create a file
file.createNewFile();
//read and write operation
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(file);
int len = ;
byte[] bytes = new byte[];
while ((len = in.read(bytes, , )) != -) {
out.write(bytes, , len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
System.out.println(UNZIP_OPERATION_SUCCESS);
if (!StringUtils.isEmpty(zipFile)) {
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return unzipFilePath + directoryName;
} /**
* 压缩文件的操作
*具体用法参考下面的main方法的用法
* @param sourceUnzipFileAbsolutePath 要压缩的文件的绝对路径,这里假定这个文件形如:a/b/c/d.jpg
* @param destZipFileAbsolutePath 压缩文件的绝对路径
*/
public static void doZip(String sourceUnzipFileAbsolutePath, String destZipFileAbsolutePath) { File unzipFile = null;
BufferedInputStream in = null;
ZipOutputStream out = null;
try {
//validate input parameters
Assert.notNull(sourceUnzipFileAbsolutePath, UNZIP_FILE_PATH_CANNOT_BE_NULL);
Assert.notNull(destZipFileAbsolutePath, ZIP_FILE_PATH_CANNOT_BE_NULL);
Assert.isTrue(destZipFileAbsolutePath.endsWith(".zip"), "zip file must ends with .zip");
unzipFile = new File(sourceUnzipFileAbsolutePath);
if (unzipFile.exists()) {
File zipFile = new File(destZipFileAbsolutePath);
if (zipFile.exists()) {
throw new RuntimeException("该zip文件已经存在,请重新输入.zip文件的绝对路径");
}
zipFile.createNewFile(); if (zipFile.exists()) {
File[] sourceFiles = unzipFile.listFiles();
if (null == sourceFiles || sourceFiles.length < ) {
System.out.println("File Catalog:" + sourceUnzipFileAbsolutePath + "nothing in there,don't have to compress!");
} else {
out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
byte[] buffers = new byte[ * ];
for (int i = ; i < sourceFiles.length; i++) {
// create .zip and put pictures in
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
out.putNextEntry(zipEntry);
// read documents and put them in the zip
in = new BufferedInputStream(new FileInputStream(sourceFiles[i]), * );
int read = ;
while ((read = in.read(buffers, , buffers.length)) != -) {
out.write(buffers, , read);
}
}
System.out.println("压缩成功,压缩文件的目录:" + zipFile.getAbsolutePath());
}
}
} else {
System.out.println(unzipFile.getAbsolutePath() + " doesn't not exists !");
throw new RuntimeException(unzipFile.getAbsolutePath() + " doesn't not exists !");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
try {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
*对jpg照片进行重命名操作
* @param unzipFilePath 需要重命名的文件路径
* @param userName 用户名
* @param workCode 工号
* @param orgCode 结构代码
*/
public static void doRename(String unzipFilePath,String userName,String workCode,String orgCode){
try {
Assert.notNull(unzipFilePath,"file cannot be null");
Assert.notNull(userName,"user name cannot be null");
Assert.notNull(workCode,"work code cannot be null");
Assert.notNull(orgCode,"organization code cannot be null");
File unzipFile = new File(unzipFilePath);
File[] files = unzipFile.listFiles();
int count=;
for(File file :files){
String destFilePath = file.getParent()+"/";
String destFileName=userName+count+"-"+workCode+count+"-"+orgCode+".jpg";
file.renameTo(new File(destFilePath+destFileName));
count++;
}
System.out.println("rename successfully !");
} catch (Exception e) {
e.printStackTrace();
System.out.println("rename failed !");
} finally {
}
}
public static void main(String[] args) {
//压缩文件的操作测试
String zipPath_zip_operation = "C:\\Users\\yckj0911\\Desktop\\yyyy.zip";
String unzipPath_zip_operation = "C:\\Users\\yckj0911\\Desktop\\人民2\\人民";
// doZip(unzipPath_zip_operation, zipPath_zip_operation); //解压文件的操作测试:
String zipPath_unzip_operation = "C:\\Users\\yckj0911\\Desktop\\yyyy.zip";
String unzipPath_unzip_operation = "C:\\Users\\yckj0911\\Desktop\\ccc";
if(!unzipPath_unzip_operation.endsWith(System.getProperty("file.separator"))){
unzipPath_unzip_operation=unzipPath_unzip_operation+System.getProperty("file.separator");
}
//doUnzip(zipPath_unzip_operation,unzipPath_unzip_operation);
String need_rename_file_path= "C:\\Users\\yckj0911\\Desktop\\ccc";
doRename(need_rename_file_path,"qls","yckj","YCKJ");
}
}

对zip文件进行解压操作和对一个文件进行压缩操作的更多相关文章

  1. windows phone使用sharpcompress进行解压压缩文件

    在做移动端时,当我们需要从服务器获得多个文件时,为了节约流量,服务器一般会返回一个压缩包,那我们就是下载完成后,在手机中进行解压到指定位置 SharpCompress就是可以在手机中进行解压一个类库( ...

  2. WebApi系列~对HttpClient的响应流进行解压

    回到目录 有时我们的请求头为ContentEncoding添加了gzip进行了压缩,而服务端返回数据时也会对它进行gzip压缩,如果在这种情况下,你直接头响应流会是乱码,而必须先进行压缩,大叔将这块的 ...

  3. PHP文件操作 之往一个文件写入数据

    //打开一个文件 $f = fopen($filename,'wb'); $filename:打开一个文件,不存在则自动创建,如果不能创建,说明指定的文件目录有错误 wb:写入的方式 ---- 覆盖原 ...

  4. shell脚本就是由Shell命令组成的执行文件,将一些命令整合到一个文件中,进行处理业务逻辑,脚本不用编译即可运行。它通过解释器解释运行,所以速度相对来说比较慢。

    shell脚本?在说什么是shell脚本之前,先说说什么是shell. shell是外壳的意思,就是操作系统的外壳.我们可以通过shell命令来操作和控制操作系统,比如Linux中的Shell命令就包 ...

  5. 工具:从一个文件夹中复制jar到另一个文件夹中

    工具类:从一个文件夹中复制jar到另一个文件夹中 需要的小伙伴可以试一试,很爽哦,有时候真的很需要! 需求:当我们拿到一个maven项目时,而maven项目的jar包都是通过pom.xml文件管理的, ...

  6. SQLSERVER将一个文件组的数据移动到另一个文件组

    SQLSERVER将一个文件组的数据移动到另一个文件组 有经验的大侠可以直接忽视这篇文章~ 这个问题有经验的人都知道怎麽做,因为我们公司的数据量不大没有这个需求,也不知道怎麽做实验 今天求助了QQ群里 ...

  7. 在C#中如何确定一个文件是不是文本文件,以及如何确定一个文件的类型

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:在C#中如何确定一个文件是不是文本文件,以及如何确定一个文件的类型.

  8. PHP文件操作 之读取一个文件(以二进制只读的方式打开)

    最近应用了文件的读取,顺便复习一下! //读取一个文件 $f = fopen($filename,'rb'); $f: 表示返回的一个资源句柄 $filename:要打开的文件路径 rb:参数,表示只 ...

  9. java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

随机推荐

  1. Date()日期函数浏览器兼容问题踩坑

    原文:Date()日期函数浏览器兼容问题踩坑 之前用layui做的一项目中,table中用到了日期格式化的问题.直接没多想,撸代码就完了呗,结果最近一段时间客户反馈说显示日期跟录入日期不一样(显示日期 ...

  2. 一步一步学Linq to sql(六):探究特性

    延迟执行 IQueryable query = from c in ctx.Customers select c; 这样的查询句法不会导致语句立即执行,它仅仅是一个描述,对应一个SQL.仅仅在需要使用 ...

  3. EIP权限工作流平台总结-1总体说明

      预览地址:www.eipflow.com (1) 权限工作流:www.demo.eipflow.com/Account/Login (2) 基础权限版:www.auth.eipflow.com/A ...

  4. bootstrap重新设计checkbox样式

    文章采集于: https://www.cnblogs.com/GumpYan/p/7845445.html#undefined 在原文基础上修改了勾勾的内容,直接采用bootstrap字体库.修改了横 ...

  5. javascript-es6学习笔记

    es6技术培训文档 第一阶段:1.let与const用法2.变量的解构赋值3.字符串的扩展4.正则的扩展5.数组的扩展6.函数的扩展7.对象的扩展8.Symbol9.Set和Map数据结构 第二阶段: ...

  6. Bash中使用MySQL导入导出CSV格式数据[转]

    转自: http://codingstandards.iteye.com/blog/604541 MySQL中导出CSV格式数据的SQL语句样本如下:   select * from test_inf ...

  7. 用gradle编译任意结构的Android项目

    ## 需求 * 继续用`Eclipse`项目的结构,但是使用`gradle`编译,或者说任意的项目结构进行编译. ## 解决方案 1. Android studio的项目结构 1. Android S ...

  8. 第三篇 Python执行方式和变量初始

    第一个Python程序 可以打开notepad或者其他文本编辑器,输入:print("Hello Python!"),将文件保存到任意盘符下,后缀名是  .py 两种python程 ...

  9. eclipse返回快捷键

    1.图上第一个箭头(Ctrl + Q) 返回上一个编辑点(编辑,修改代码) 2.图上第二个箭头(Alt + Left) 返回上一个操作点(点击进入方法等操作) 3.图上第三个箭头(Alt + Righ ...

  10. 为什么mysqld启动报错

    在一台ubuntu测试机器上启动一个mysql实例,本来应该是一件很简单的事情, 启动的时候却报错了:   mysqld_safe --defaults-file=/etc/mysql/my3307. ...