package com.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
/**
* 解压缩文件
*
*/
public class Zip extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 5142373267185568029L;
/**
* 压缩文件为zip格式
*
* @paramzipFile 压缩文件后的文件名
* @paramsrcPathName 待压缩文件
* @throws IOException
* 文件不存在异常
*/
public static void zipFile(String srcPathName) throws IOException {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "not exist!");
//System.out.println(srcPathName);
FileOutputStream fileOutputStream = new FileOutputStream(srcPathName+".zip");
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.flush();
out.close();
}
private static void compress(File file, ZipOutputStream out, String basedir) throws IOException {
if (file.isDirectory()) {
compressDirectory(file, out, basedir);
} else {
compressFile(file, out, basedir);
}
}
private static void compressDirectory(File dir, ZipOutputStream out, String basedir) throws IOException {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
compress(files[i], out, basedir + dir.getName() + "/");
}
}
private static void compressFile(File file, ZipOutputStream out, String basedir) throws IOException {
if (!file.exists()) {
return;
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int len = 0;
byte data[] = new byte[1024];
while ((len = bis.read(data)) != -1) {
out.write(data, 0, len);
}
bis.close();
}
private static void createDir(String path) {
File dir = new File(path);
if (dir.exists() == false)
dir.mkdir();
}
private static String getSuffixName(String name) {
return name.substring(0, name.lastIndexOf("."));
}
/**
* 解压缩文件
* @throws IOException
* 文件读写异常
*/
public static void unZip(String zipFilePath) throws IOException {
File file = new File(zipFilePath+".zip");
ZipFile zipFile = new ZipFile(file);
File unzipFile = new File(zipFilePath);
if (unzipFile.exists())
unzipFile.delete();
unzipFile.mkdirs();
Enumeration zipEnum = zipFile.entries();
InputStream input = null;
OutputStream output = null;
ZipEntry entry = null;
while (zipEnum.hasMoreElements()) {
entry = (ZipEntry) zipEnum.nextElement();
String entryName = new String(entry.getName());
String names[] = entryName.split("\\/");
int length = names.length;
String path = unzipFile.getAbsolutePath();
for (int v = 0; v < length; v++) {
if (v < length - 1) {
path += "/" + names[v] + "/";
createDir(path);
} else {
if (entryName.endsWith("/"))
createDir(unzipFile.getAbsolutePath() + "/" + entryName);
else {
input = zipFile.getInputStream(entry);
output = new FileOutputStream(new File(unzipFile.getAbsolutePath() + "/" + entryName));
byte[] buffer = new byte[1024 * 8];
int readLen = 0;
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
output.write(buffer, 0, readLen);
input.close();
output.flush();
output.close();
}
}
}
}
}
public static void main(String[] args) {
Zip zip=new Zip();
//String path=zip.getClassPath();//System.getProperty("user.dir");
try {
String path=zip.findServerPath();
//System.out.println(path);
//压缩
//zip.zipFile(path+"testzip");//路径+文件名
// 解压
//zip.unZip(path+"testzip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取服务器端的webapps路径
* @return
*/
public String findServerPath() throws Exception{
String path=this.getServletContext().getRealPath("/");
return path;
}
/**
* 得到本类物理路径所在文件夹
* @return
*/
private String getClassPath()throws Exception{
String strClassName = getClass().getName();
String strPackageName = "";
if(getClass().getPackage() != null) {
strPackageName = getClass().getPackage().getName();
}
String strClassFileName = "";
if(!"".equals(strPackageName)){
strClassFileName = strClassName.substring(strPackageName.length() + 1,strClassName.length());
}
else {
strClassFileName = strClassName;
}
URL url = null;
url = getClass().getResource(strClassFileName + ".class");
String strURL = url.toString();
//String middleString = System.getProperty("file.separator"); // 取得操作系统路径分割符
strURL = strURL.substring(strURL.indexOf( "/" ) + 1,strURL.lastIndexOf( "/" ));
return strURL;
}
}
分享到:
相关推荐
java压缩解压文件
安卓端java代码实现对文件的压缩与解压
JAVA压缩解压JAVA压缩解压JAVA压缩解压JAVA压缩解压JAVA压缩解压
Java压缩及解压tar、tar.z格式文件, 需要apache的包ant-1.7.1.jar 这个自己去搜索下下载
JAVA文件压缩与解压缩实践,java解压缩zip文件,Java源码
NULL 博文链接:https://zhaoshunxin.iteye.com/blog/1336238
JAVA文件压缩与解压缩实践(源代码+论文)
java压缩文件成zip实现无乱码 内置类与jar包
java 解压缩zip文件 java 解压缩zip文件 java 解压缩zip文件
(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业...
java程序,压缩与解压rar文件. java程序,压缩与解压rar文件.
对zip文件进行解压缩工具类,以及jar包
使用java解压缩文件,并获取压缩文件中的文件名称,使用多线程,提高效率
JAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩...
JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件压缩与解压缩实践(源代码+论文) JAVA文件...
java解压zip压缩文件,读取压缩文件中的内容并输出到输出文件中。
代码实现解压,压缩linux下的GZ格式文件
工具类中提供的方法有:压缩单个文件,压缩多个文件,解压文件到制定路径下。获取压缩文件的文件列表。压缩文件的文件注释。