java 解压缩代码
2015-10-14 10:59
411 查看
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Decompression {
public static void main(String[] args) {
String tat = "F:\\WDJDownload\\question.zip";
String tatout = "F:\\WDJDownload\\out\\";
unzipZipFile(tat, tatout);
}
public static final void writeFile(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
public static void unzipZipFile(String zipFileName, String directoryToExtractTo) {
@SuppressWarnings("rawtypes")
Enumeration entriesEnum;
ZipFile zipFile;
try {
zipFile = new ZipFile(zipFileName);
entriesEnum = zipFile.entries();
File directory = new File(directoryToExtractTo);
if (!directory.exists()) {
directory.mkdir();
System.err.println("...Directory Created -" + directoryToExtractTo);
}
while (entriesEnum.hasMoreElements()) {
try {
ZipEntry entry = (ZipEntry)entriesEnum.nextElement();
if (entry.isDirectory()) {
/**
* Currently not unzipping the directory structure.
* All the files will be unzipped in a Directory
*
**/
} else {
System.err.println("Extracting file: " + entry.getName());
/**
* The following logic will just extract the file name
* and discard the directory
*/
int index = 0;
String name = entry.getName();
index = entry.getName().lastIndexOf("/");
if (index > 0 && index != name.length())
name = entry.getName().substring(index + 1);
System.out.println(name);
writeFile(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(directoryToExtractTo + name)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
zipFile.close();
} catch (IOException ioe) {
System.err.println("Some Exception Occurred:");
ioe.printStackTrace();
return;
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Decompression {
public static void main(String[] args) {
String tat = "F:\\WDJDownload\\question.zip";
String tatout = "F:\\WDJDownload\\out\\";
unzipZipFile(tat, tatout);
}
public static final void writeFile(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
public static void unzipZipFile(String zipFileName, String directoryToExtractTo) {
@SuppressWarnings("rawtypes")
Enumeration entriesEnum;
ZipFile zipFile;
try {
zipFile = new ZipFile(zipFileName);
entriesEnum = zipFile.entries();
File directory = new File(directoryToExtractTo);
if (!directory.exists()) {
directory.mkdir();
System.err.println("...Directory Created -" + directoryToExtractTo);
}
while (entriesEnum.hasMoreElements()) {
try {
ZipEntry entry = (ZipEntry)entriesEnum.nextElement();
if (entry.isDirectory()) {
/**
* Currently not unzipping the directory structure.
* All the files will be unzipped in a Directory
*
**/
} else {
System.err.println("Extracting file: " + entry.getName());
/**
* The following logic will just extract the file name
* and discard the directory
*/
int index = 0;
String name = entry.getName();
index = entry.getName().lastIndexOf("/");
if (index > 0 && index != name.length())
name = entry.getName().substring(index + 1);
System.out.println(name);
writeFile(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(directoryToExtractTo + name)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
zipFile.close();
} catch (IOException ioe) {
System.err.println("Some Exception Occurred:");
ioe.printStackTrace();
return;
}
}
}
相关文章推荐
- java计算器 图形用户界面 精简版
- java中i++与++i的探讨
- 04 eclipse 自动补全功能的改进
- Java基础知识强化之IO流笔记67:Properties的特殊功能使用
- java将16进制颜色值转换为RGBA值为0.0f~1.0f的方法
- Java的JAR包, EAR包 ,WAR包 都是干什么的,有什么区别
- This is a built-in profile,change the name to create a new profile(MyEclipse)
- java日期转换成字符串,字符串转换日期
- Java方法继承、方法重载、方法覆盖,多态和super关键字小总结
- Spring AOP 详解
- JAVA操作properties文件
- Activiti系列——如何在eclipse中安装 Activiti Designer插件
- Spring 3整合Quartz 2实现定时任务
- java查找读取文件url的拼接
- Java基础知识学习(一)
- 新应用的知识整理-java代码中完成布局
- java forEach实现原理
- myeclipse中svn不显示
- java中静态代码块的用法 static用法详解
- Java基础知识强化之IO流笔记66:Properties的概述 和 使用(作为Map集合使用)