注意这里用的是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. 008---re正则模块

    re正则模块 字符串的匹配规则 匹配模式 re.match() re.search() re.findall() re.split() re.sub() 元字符 print('------------ ...

  2. Python tips(

    (此文是在实际工程中遇到的一些小问题,给予解决和整理.解决方法大多来自网上零散的文章.有一个系统化的Python问题解决方案,来自<Python 3 学习笔记>雨痕著,其中对Python的 ...

  3. (数据科学学习手札13)K-medoids聚类算法原理简介&Python与R的实现

    前几篇我们较为详细地介绍了K-means聚类法的实现方法和具体实战,这种方法虽然快速高效,是大规模数据聚类分析中首选的方法,但是它也有一些短板,比如在数据集中有脏数据时,由于其对每一个类的准则函数为平 ...

  4. Python | 用Pyinstaller打包发布exe应用

    参考:https://jingyan.baidu.com/article/a378c960b47034b3282830bb.html https://ask.csdn.net/questions/72 ...

  5. 简单整理React的Context API

    之前做项目时经常会遇到某个组件需要传递方法或者数据到其内部的某个子组件,中间跨越了甚至三四层组件,必须层层传递,一不小心哪层组件忘记传递下去了就不行.然而我们的项目其实并没有那么复杂,所以也没有使用r ...

  6. ubuntu配置机器学习环境(四) 安装intel MKL

    在这一模块可以选择(ATLAS,MKL或者OpenBLAS),我这里使用MKL,首先下载并安装英特尔® 数学内核库 Linux* 版MKL,下载链接, 请下载Student版,先申请,然后会立马收到一 ...

  7. 当app出现线上奔溃,该如何办?

    1.如何追踪app崩溃率,如何解决线上闪退 当iOS设备上的App应用闪退时,操作系统会生成一个crash日志,保存在设备上.crash日志上有很多有用的信息,比如每个正在执行线程的完整堆栈跟踪信息和 ...

  8. 初步学习pg_control文件之九

    接前文,初步学习pg_control文件之八 来看这个: pg_time_t time; /* time stamp of last pg_control update */ 当初初始化的时候,是这样 ...

  9. 根据生产场景对Linux系统进行分区

    转自:http://oldboy.blog.51cto.com/2561410/629558 老鸟谈生产场景如何对linux系统进行分区? █  前言:    我们买房子时,会考虑1室1厅,2室1厅, ...

  10. 用起来超爽的Maven——入门篇

    你还在为怎样寻找.导入SSH相关依赖包纠结吗? 你还在为没有安装IDE开发工具不能编译.部署.运行项目而纠结吗? 你还在为公司项目目录结构怎样规范而纠结吗? 亲爱的纠结哥,只要你使用了Maven,一切 ...