`
- 浏览:
116040 次
- 性别:
- 来自:
广州
-
转自:http://wlh.iteye.com/
-
import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- import org.apache.tools.zip.ZipOutputStream;
-
- public class CompressFile {
-
private static CompressFile instance = new CompressFile();
-
-
private CompressFile() {
-
}
-
-
public static CompressFile getInstance() {
-
return instance;
-
}
-
-
public synchronized void zip(String inputFilename, String zipFilename)
-
throws IOException {
-
zip(new File(inputFilename), zipFilename);
-
}
-
-
public synchronized void zip(File inputFile, String zipFilename) throws IOException {
-
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
-
zipFilename));
-
-
try {
-
zip(inputFile, out, "");
-
} catch (IOException e) {
-
throw e;
-
} finally {
-
out.close();
-
}
-
}
-
-
private synchronized void zip(File inputFile, ZipOutputStream out, String base)
-
throws IOException {
-
if (inputFile.isDirectory()) {
-
File[] inputFiles = inputFile.listFiles();
-
out.putNextEntry(new ZipEntry(base + "/"));
-
base = base.length() == 0 ? "" : base + "/";
-
for (int i = 0; i < inputFiles.length; i++) {
-
zip(inputFiles[i], out, base + inputFiles[i].getName());
-
}
-
-
} else {
-
if (base.length() > 0) {
-
out.putNextEntry(new ZipEntry(base));
-
} else {
-
out.putNextEntry(new ZipEntry(inputFile.getName()));
-
}
-
-
FileInputStream in = new FileInputStream(inputFile);
-
try {
-
int c;
-
byte[] by = new byte[BUFFEREDSIZE];
-
while ((c = in.read(by)) != -1) {
-
out.write(by, 0, c);
-
}
-
} catch (IOException e) {
-
throw e;
-
} finally {
-
in.close();
-
}
-
}
-
}
-
-
public synchronized void unzip(String zipFilename, String outputDirectory)
-
throws IOException {
-
File outFile = new File(outputDirectory);
-
if (!outFile.exists()) {
-
outFile.mkdirs();
-
}
-
-
ZipFile zipFile = new ZipFile(zipFilename);
-
Enumeration en = zipFile.getEntries();
-
ZipEntry zipEntry = null;
-
while (en.hasMoreElements()) {
-
zipEntry = (ZipEntry) en.nextElement();
-
if (zipEntry.isDirectory()) {
-
-
String dirName = zipEntry.getName();
-
dirName = dirName.substring(0, dirName.length() - 1);
-
-
File f = new File(outFile.getPath() + File.separator + dirName);
-
f.mkdirs();
-
-
} else {
-
-
File f = new File(outFile.getPath() + File.separator
-
+ zipEntry.getName());
-
f.createNewFile();
-
InputStream in = zipFile.getInputStream(zipEntry);
-
FileOutputStream out = new FileOutputStream(f);
-
try {
-
int c;
-
byte[] by = new byte[BUFFEREDSIZE];
-
while ((c = in.read(by)) != -1) {
-
out.write(by, 0, c);
-
}
-
-
} catch (IOException e) {
-
throw e;
-
} finally {
-
out.close();
-
in.close();
-
}
-
}
-
}
-
}
-
-
private static final int BUFFEREDSIZE = 1024;
-
-
public static void main(String[] args) {
-
CompressFile bean = new CompressFile();
-
try {
-
bean.zip("d:/temp", "d:/test.zip");
-
-
bean.unzip("d:/test.zip", "d:/out/temp");
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
用Java压缩解压ZIP文件,将利用java.util.zip 包中提供的类来实现压缩和解压zip 格式文件的功能。当然,本例在功能上完全没有Winzip 等成熟的压缩软件那么强,也不能做的很强,本例仅仅是演示如何来使用java.util....
JAVA文件压缩与解压缩实践,java解压缩zip文件,Java源码
java解压zip压缩文件,读取压缩文件中的内容并输出到输出文件中。
java压缩文件成zip实现无乱码 内置类与jar包
java压缩解压ZIP
java控制指定路径下的文件夹压缩成zip格式。 java将zip压缩包解压成为文件夹。直接使用io流进行操作,不需借助其他jar包
对zip文件进行解压缩工具类,以及jar包
java 操作Zip文件(压缩、解压、加密) zip4j-1.3.2.jar ant-1.10.6.jar
JAVA文件压缩与解压缩实践(源代码+论文)
压缩文件方法 该方法需要引用zip4j的jar文件 单个文件、多个文件压缩 ... * @throws ZipException 压缩文件有损坏或者解压缩失败抛出 */ 方法详细见文件! 一个简单的demo 欢迎大家指点,一起提升
zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java
Java实现的带密码压缩与解压zip文件源码,可将多个文件使用不同的密码压进同一个zip文件;可以解压带密码的zip文件(store压缩模式)
java 中压缩ZIP格式源码,仅供参考。
使用 Java 语言实现并封装的创建 ZIP 格式的压缩文件并解压到指定目录和解压 ZIP 文件到指定目录的工具类。
java 解压缩zip文件 java 解压缩zip文件 java 解压缩zip文件
java 压缩/解压 .zip/.rar/.tar 文件,大家一起讨论,学习 已实现zip压缩/解压,rar解压
java解压缩zip代码与用到的jar包
(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业设计)JAVA文件压缩与解压缩实践(Java毕业...
使用java解压缩文件,并获取压缩文件中的文件名称,使用多线程,提高效率