对zip文件进行解压操作和对一个文件进行压缩操作
注意这里用的是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文件进行解压操作和对一个文件进行压缩操作的更多相关文章
- windows phone使用sharpcompress进行解压压缩文件
在做移动端时,当我们需要从服务器获得多个文件时,为了节约流量,服务器一般会返回一个压缩包,那我们就是下载完成后,在手机中进行解压到指定位置 SharpCompress就是可以在手机中进行解压一个类库( ...
- WebApi系列~对HttpClient的响应流进行解压
回到目录 有时我们的请求头为ContentEncoding添加了gzip进行了压缩,而服务端返回数据时也会对它进行gzip压缩,如果在这种情况下,你直接头响应流会是乱码,而必须先进行压缩,大叔将这块的 ...
- PHP文件操作 之往一个文件写入数据
//打开一个文件 $f = fopen($filename,'wb'); $filename:打开一个文件,不存在则自动创建,如果不能创建,说明指定的文件目录有错误 wb:写入的方式 ---- 覆盖原 ...
- shell脚本就是由Shell命令组成的执行文件,将一些命令整合到一个文件中,进行处理业务逻辑,脚本不用编译即可运行。它通过解释器解释运行,所以速度相对来说比较慢。
shell脚本?在说什么是shell脚本之前,先说说什么是shell. shell是外壳的意思,就是操作系统的外壳.我们可以通过shell命令来操作和控制操作系统,比如Linux中的Shell命令就包 ...
- 工具:从一个文件夹中复制jar到另一个文件夹中
工具类:从一个文件夹中复制jar到另一个文件夹中 需要的小伙伴可以试一试,很爽哦,有时候真的很需要! 需求:当我们拿到一个maven项目时,而maven项目的jar包都是通过pom.xml文件管理的, ...
- SQLSERVER将一个文件组的数据移动到另一个文件组
SQLSERVER将一个文件组的数据移动到另一个文件组 有经验的大侠可以直接忽视这篇文章~ 这个问题有经验的人都知道怎麽做,因为我们公司的数据量不大没有这个需求,也不知道怎麽做实验 今天求助了QQ群里 ...
- 在C#中如何确定一个文件是不是文本文件,以及如何确定一个文件的类型
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:在C#中如何确定一个文件是不是文本文件,以及如何确定一个文件的类型.
- PHP文件操作 之读取一个文件(以二进制只读的方式打开)
最近应用了文件的读取,顺便复习一下! //读取一个文件 $f = fopen($filename,'rb'); $f: 表示返回的一个资源句柄 $filename:要打开的文件路径 rb:参数,表示只 ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
随机推荐
- 算法竞赛入门经典-1.5.4 Q&A
这小节考察实践能力,要求在不要查书.不要网上找答案,自己用实验的方法解决以下五个问题: 做这五道题时,好几道都没思路,违反了规则到网上找了一圈,居然没找到答案,于是打算写这篇博客.不知是否有更好的实践 ...
- (数据科学学习手札01)Python与R基本数据结构之异同
Python 1.列表(list) list1 = [i for i in range(10)] list1[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 特点:可遍历,可索引,可切片 ...
- BFS 队列
Plague Inc. is a famous game, which player develop virus to ruin the world. JSZKC wants to model thi ...
- ajax同步和异步的切换
ajax为网页提供了非常不错的异步机制,但是有时候两个ajax放在一起,希望第一个完成后再继续第二个ajax的执行.这时候可以将第一个ajax代码带上同步参数即可,如下: $.ajax({ async ...
- MyEclipse - 问题集 - Java compiler level does not match the version of the installed Java project facet
右键项目“Properties”,在弹出的“Properties”窗口左侧,单击“Project Facets”,打开“Project Facets”页面. 在页面中的“Java”下拉列表中,选择相应 ...
- error LNK2001: unresolved external symbol "public: __thiscall ControllerInterface::ControllerInterface(class QObject *)" (??0ControllerInterface@@QAE@PAVQObject@@@Z) downloadcontroller.obj
前几天刚遇到这个问题,但是今天再碰到就又要思考怎么解决.这次特别记录一下,以防下次碰到再手足无措: 1.看到这个报错第一感觉LNK关键字,表示连接错误,这种错误有几个可以下手的点 1)函数声明和定义是 ...
- Yarn 命令详解
常用命令 创建项目:yarn init 安装依赖包:yarn == yarn install 添加依赖包:yarn add 配置淘宝镜像:yarn config set registry " ...
- [HNOI2004]打鼹鼠
鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿牛编写了一个打鼹鼠的游戏:在一个\(n*n\)的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气.你 ...
- extjs/js时间校验
//时间秒判断var re=/^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:( ...
- tomcat8编码设置和gc异常解决
用startup.bat启动 编码解决: 用编辑器打开catalina.bat文件找到set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS% " 更改为 se ...