对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 ...
随机推荐
- unity开发c#代码
1.摄像头跟随主角移动,并支持旋转. 开发过程中需要摄像头以一定距离跟随player,同时会进行旋转,属于一种常见的跟随方式. using UnityEngine; using System.Coll ...
- linux基础重要命令小节
此为L005&&L006课程内容的一个总结. 命令: 基本形式 命令 [参数] [路径或文件] 例:ls -ld /data pwd 目前所在目录 [root@moban /]# pw ...
- android开源项目之OTTO事件总线(二)官方demo解说
官方demo见 https://github.com/square/otto 注意自己该编译版本为2.3以上,默认的1.6不支持match_parent属性,导致布局文件出错. 另外需要手动添加an ...
- SGU 101 Domino 题解
鉴于SGU题目难度较大,AC后便给出算法并发布博文,代码则写得较满意后再补上.——icedream61 题目简述:暂略 AC人数:3609(2015年7月20日) 算法: 这题就是一笔画,最多只有7个 ...
- Sqlite Datetime类型详解
日期和时间函数 date(timestring, modifier, modifier, ...) time(timestring, modifier, modifier, ...) datetime ...
- python接口自动化: CAS系统验证,自动完成登录并获取token,遇到302请求重定向设置(requests模块 allow_redirects=False)即可
import requestsimport re import requests import re class Crm_token(object): try: username=int(input( ...
- 《python核心编程第二版》第2章习题
2-1 略 2-1 略 2-2 (a)打印 结果是9 (b)9 (c)一样 (d)略 (e)略 2-3 略 2-4 (a) # /usr/bin/pythonraw_input() (b) # /us ...
- 从传统IT快速走向公共云计算
2年前有篇报道,说Facebook的每个运维同学至少能管理2万台服务器,这在当时的国内互联网引起了很大震动,按照传统IT的理解,每个运维同学能管理200台服务器已经很了不起了. 这些年来云计算发展非常 ...
- Faster RCNN代码解析
1.faster_rcnn_end2end训练 1.1训练入口及配置 def train(): cfg.GPU_ID = 0 cfg_file = "../experiments/cfgs/ ...
- HTML5的 input:file上传类型控制(转载)
http://www.haorooms.com/post/input_file_leixing HTML5的 input:file上传类型控制 2014年8月29日 66352次浏览 一.input: ...