package com.highguard.file.common;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtils {
public static void main(String[] args) {
//压缩目录为zip
zip ("d:/temp/", "d:/temp/ym.zip");
//解压缩zip文件到目录
unZip("d:/temp/ym.zip", "d:/temp/zip");
}
public static void zip(String sourceDir, String zipFile) {
try {
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
File file = new File(sourceDir);
String basePath = null;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void zipFile(File source, String basePath,
ZipOutputStream zos) {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
String pathName;
byte[] buf = new byte[1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1)
+ "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
is.close();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void unZip(String zipfile, String destDir) {
destDir = destDir.endsWith("\\") ? destDir : destDir + "\\";
byte b[] = new byte[1024];
int length;
ZipFile zipFile;
try {
zipFile = new ZipFile(new File(zipfile));
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + zipEntry.getName());
if (zipEntry.isDirectory()) {
// 这段都可以不要,因为每次都貌似从最底层开始遍历的
loadFile.mkdirs();
} else {
if (!loadFile.getParentFile().exists())
loadFile.getParentFile().mkdirs();
OutputStream outputStream = new FileOutputStream(loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);
}
}
System.out.println(" 文件解压成功 ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
分享到:
相关推荐
java 解压缩zip文件 java 解压缩zip文件 java 解压缩zip文件
JAVA文件压缩与解压缩实践,java解压缩zip文件,Java源码
java 中压缩ZIP格式源码,仅供参考。
使用java实现解压缩zip文件,有详细的注释。
zip格式是一种免费的压缩文件格式,我们可以通过java类库编写函数来解压缩它,实现winwar这类软件的功能。
JAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩实践(源代码).zipJAVA文件压缩与解压缩...
JAVA实现解压缩ZIP格式的压缩文件(含中文名文件)的完整DEMO,含用到的JAR包。
java语言操作解压缩文件。 /** * 数据压缩 * * @param data * @return * @throws Exception */ public static byte[] compress(byte[] data) throws Exception { ByteArrayInputStream bais = new ...
博文链接:https://peng-jian-ming.iteye.com/blog/190572
本系统运用LZ77字典算法、懒惰匹配算法和Huffman编码算法,使用Java语言在Jbuilder2006环境下设计了使用GZIP算法对文件压缩与解压缩的实现程序。用户可以根据自己的需求,使用此程序方便地对文件进行压缩或者解压缩...
通过后台上传一个压缩文件然后直接通过代码的处理将文件解压缩
java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文).zip java毕业设计——java文件压缩与解压缩实践设计与开发(源代码+论文)....
java 实现压缩与解压缩ZIP .docjava 实现压缩与解压缩ZIP .docjava 实现压缩与解压缩ZIP .docjava 实现压缩与解压缩ZIP .doc
NULL 博文链接:https://hogwartsrow.iteye.com/blog/1853355
poi.zip java读取excel文件,其中bin压缩文件中含有jar包,eclipse导入时可直接使用;src压缩文件含有java源代码,需要阅读源码的童鞋可以下载使用
JAVA文件压缩与解压缩实践(源代码+论文)
java zip文件的压缩与解压缩,支持中文 使用apache的工具包可设置字符编码
解决java zip压缩文件里面带有中文文件,在java代码解压时报错,或者无法解压问题