注意这里用的是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. Leecode刷题之旅-C语言/python-9.回文数

    /* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...

  2. Eclipse报错:An internal error occurred during: "Building workspace". Java heap space),卡死解决办法

    在项目工程的根目录下,找到.project,用记事本打开,把两处删除掉: 第一处: <buildCommand> <name>org.eclipse.wst.jsdt.core ...

  3. 【转】谈谈 iOS 中图片的解压缩

    转自:http://blog.leichunfeng.com/blog/2017/02/20/talking-about-the-decompression-of-the-image-in-ios/ ...

  4. RelativeSource设定绑定方向

    <Window x:Class="Yingbao.Chapter2.RelativeEx.AppWin" xmlns="http://schemas.microso ...

  5. 从C到C++ (3)

    从C到C++ (3) 一.    C++中增加了引用 1.引用是给某一个变量起别名.引用的一般格式: 类型 &引用名 = 变量名 定义引用时一定要初始化.在实际应用中,引用一般用作参数传递与返 ...

  6. 类 java.util.Collections 提供了对Set、List、Map进行排序、填充、查找元素的辅助方法。

      类 java.util.Collections 提供了对Set.List.Map进行排序.填充.查找元素的辅助方法. 1. void sort(List) //对List容器内的元素排序,排序的规 ...

  7. 理解JAVA常量池

    下面是一些String相关的常见问题: String中的final用法和理解final StringBuffer a = new StringBuffer("111");final ...

  8. Web框架本质及浅谈HTTP协议

    Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 impor ...

  9. npx 命令介绍

    这个是在 npmv5.2.0引入的一条命令(查看),引入这个命令的目的是为了提升开发者使用包内提供的命令行工具的体验. 为什么引入这个命令 举个例子,我们开发中要运行 parcel 命令来打包:par ...

  10. final static 修饰(转载)

    static修饰符        static修饰符能够与属性.方法和内部类一起使用,表示静态的.类中的静态变量和静态方法能够与类名一起使用,不需要创建一个类的对象来访问该类的静态成员,所以,stat ...