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 API来处理ZIP文件格式。 在Java中,我们可以利用内置的`java.util.zip`包来处理压缩和解压操作。`ZipOutputStream`和`ZipInputStream`是这个包中的...
在Android中压缩文件,我们需要创建一个`ZipOutputStream`对象,然后通过它逐个添加文件到ZIP文件中。以下是一个基本的步骤: 1. 打开一个输出流到目标ZIP文件。 2. 创建`ZipOutputStream`,将输出流作为参数传入。...
在Java编程语言中,文件的压缩与解压缩是常见的数据处理操作,特别是在数据传输、存储优化和备份场景中。本实践项目围绕这个主题展开,包括源代码和相关的论文,为学习者提供了深入理解和应用Java压缩库的机会。以下...
虽然Ant不直接用于解压缩文件,但可以与Java代码结合使用,例如通过Ant任务来调用`CompressFileUtils`进行解压缩操作。Ant提供了一系列的任务,如`unzip`,可以直接在构建脚本中用于解压缩文件。 至于标签“文件解...
Java在Linux环境下对GZ压缩文件的处理是一个常见的任务,特别是在处理大文件或者网络传输时。GZ是一种常用的文件压缩格式,它利用了gzip程序进行压缩,而在Java中,我们可以使用`java.util.zip`包中的类来实现对GZ...
Java压缩及解压tar、tar.z格式文件, 需要apache的包ant-1.7.1.jar 这个自己去搜索下下载
3. **解压缩文件** 解压ZIP文件则需要一个`ZipInputStream`实例,它包装在一个`FileInputStream`中,指向ZIP文件。通过调用`getNextEntry`方法,你可以逐个访问ZIP文件中的每个条目,然后创建相应的文件或目录,将...
这篇博客“java ftp上传 下载 文件压缩解压”很可能是关于如何使用Java实现FTP文件上传、下载以及文件的压缩与解压功能。下面我们将深入探讨这些知识点。 首先,FTP上传和下载是Java中常见的任务,通常通过`java...
在提供的代码片段中,`unzipFile`方法展示了如何解压缩一个`.zip`文件。它首先创建一个`ZipFile`对象,然后遍历其中的所有`ZipEntry`,如果是目录,则创建对应的目录结构;如果是文件,则通过`getInputStream`获取流...
zipEntry.setComment("压缩文件"); // 可以添加注释,同样需要指定字符集 zos.putNextEntry(zipEntry); try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { byte[] buffer...
3. **解压缩文件**:与压缩相反,`unzipFiles()`方法可能利用`ZipInputStream`来读取ZIP文件并解压到指定目录。通过循环遍历输入流中的每个`ZipEntry`,创建对应的目标文件,然后将其内容复制出来。 4. **路径和...
(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业...
在这个场景中,我们关注的是如何使用Java来实现文件压缩包的上传以及在服务器上的解压缩。Java作为一种强大的编程语言,提供了丰富的库来支持这样的操作,特别是在涉及到SFTP(Secure File Transfer Protocol)时,...
ame()); tOut.putArchiveEntry(tarEntry);...通过引入该库,我们可以轻松地在 Java 程序中实现文件和文件夹的压缩与解压缩功能。在实际开发中,注意错误处理、资源管理以及安全性等方面,以确保程序的健壮性和安全性。
Java自动解压缩文件是编程领域中的一个重要话题,尤其是在服务器端应用中,经常需要处理上传的压缩文件并进行解压操作。Java提供了丰富的API来支持这一功能,主要涉及到`java.util.zip`包中的类,如`ZipInputStream`...
- **递归压缩文件**:对于每个文件或目录,如果是目录,则递归地对其子文件进行压缩;如果是文件,则直接压缩。 - **关闭资源**:完成压缩后,需要关闭所有打开的输入输出流。 ##### 2. 关键代码解读 ```java ...