Java解压zip文件
2010-04-13 11:43
381 查看
package com.sunz.fileUpload;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import sun.io.*;
public class Linzip {
public static String make8859toGB(String str) {
try {
String str8859 = new String(str.getBytes("8859_1"), "GB2312");
return str8859;
} catch (UnsupportedEncodingException ioe) {
return str;
}
}
public static native int MessageBox(int hWnd, String lpText, String lpCaption, int uType);
public String unZip(String filePath){
String shpPath = "";
File infile = new File(filePath);
try {
// 检查是否是ZIP文件
ZipFile zip = new ZipFile(infile);
zip.close();
// 建立与目标文件的输入连接
ZipInputStream in = new ZipInputStream(new FileInputStream(infile));
ZipEntry file = in.getNextEntry();
int i = infile.getAbsolutePath().lastIndexOf('.');
String dirname = new String();
if (i != -1){
dirname = infile.getAbsolutePath().substring(0, i);
System.out.println("dirname:"+dirname);
}
else{
dirname = infile.getAbsolutePath();
System.out.println("dirname:"+dirname);
}
File newdir = new File(dirname);
newdir.mkdir();
byte[] c = new byte[1024];
int slen;
while (file != null) {
i = make8859toGB(file.getName()).replace('/', '//')
.lastIndexOf('//');
if (i != -1) {
File dirs = new File(dirname
+ File.separator
+ make8859toGB(file.getName()).replace('/', '//')
.substring(0, i));
dirs.mkdirs();
dirs = null;
}
System.out.print("Extract "
+ make8859toGB(file.getName()).replace('/', '//')
+ " ........ ");
if (file.isDirectory()) {
File dirs = new File(make8859toGB(file.getName()).replace(
'/', '//'));
dirs.mkdir();
dirs = null;
} else {
String theFilePath = dirname
+ File.separator
+ make8859toGB(file.getName()).replace('/', '//');
if(theFilePath.substring(theFilePath.lastIndexOf("."), theFilePath.length()).equals(".shp")){
shpPath = theFilePath;
System.out.println(theFilePath);
}
FileOutputStream out = new FileOutputStream(theFilePath);
while ((slen = in.read(c, 0, c.length)) != -1)
out.write(c, 0, slen);
out.close();
}
System.out.print("O.K./n");
file = in.getNextEntry();
}
in.close();
} catch (ZipException zipe) {
MessageBox(0, infile.getName() + "不是一个ZIP文件!", "文件格式错误", 16);
} catch (IOException ioe) {
MessageBox(0, "读取" + filePath + "时错误!", "文件读取错误", 16);
} catch (Exception i) {
System.out.println("over"+i.getMessage());
}
return shpPath;
}
public static void main(String[] args) {
Linzip linzip = new Linzip();
String path = linzip.unZip("d:/ssss.zip");
System.out.println("here:"+path);
}
}
import java.io.*;
import java.util.*;
import java.util.zip.*;
import sun.io.*;
public class Linzip {
public static String make8859toGB(String str) {
try {
String str8859 = new String(str.getBytes("8859_1"), "GB2312");
return str8859;
} catch (UnsupportedEncodingException ioe) {
return str;
}
}
public static native int MessageBox(int hWnd, String lpText, String lpCaption, int uType);
public String unZip(String filePath){
String shpPath = "";
File infile = new File(filePath);
try {
// 检查是否是ZIP文件
ZipFile zip = new ZipFile(infile);
zip.close();
// 建立与目标文件的输入连接
ZipInputStream in = new ZipInputStream(new FileInputStream(infile));
ZipEntry file = in.getNextEntry();
int i = infile.getAbsolutePath().lastIndexOf('.');
String dirname = new String();
if (i != -1){
dirname = infile.getAbsolutePath().substring(0, i);
System.out.println("dirname:"+dirname);
}
else{
dirname = infile.getAbsolutePath();
System.out.println("dirname:"+dirname);
}
File newdir = new File(dirname);
newdir.mkdir();
byte[] c = new byte[1024];
int slen;
while (file != null) {
i = make8859toGB(file.getName()).replace('/', '//')
.lastIndexOf('//');
if (i != -1) {
File dirs = new File(dirname
+ File.separator
+ make8859toGB(file.getName()).replace('/', '//')
.substring(0, i));
dirs.mkdirs();
dirs = null;
}
System.out.print("Extract "
+ make8859toGB(file.getName()).replace('/', '//')
+ " ........ ");
if (file.isDirectory()) {
File dirs = new File(make8859toGB(file.getName()).replace(
'/', '//'));
dirs.mkdir();
dirs = null;
} else {
String theFilePath = dirname
+ File.separator
+ make8859toGB(file.getName()).replace('/', '//');
if(theFilePath.substring(theFilePath.lastIndexOf("."), theFilePath.length()).equals(".shp")){
shpPath = theFilePath;
System.out.println(theFilePath);
}
FileOutputStream out = new FileOutputStream(theFilePath);
while ((slen = in.read(c, 0, c.length)) != -1)
out.write(c, 0, slen);
out.close();
}
System.out.print("O.K./n");
file = in.getNextEntry();
}
in.close();
} catch (ZipException zipe) {
MessageBox(0, infile.getName() + "不是一个ZIP文件!", "文件格式错误", 16);
} catch (IOException ioe) {
MessageBox(0, "读取" + filePath + "时错误!", "文件读取错误", 16);
} catch (Exception i) {
System.out.println("over"+i.getMessage());
}
return shpPath;
}
public static void main(String[] args) {
Linzip linzip = new Linzip();
String path = linzip.unZip("d:/ssss.zip");
System.out.println("here:"+path);
}
}
相关文章推荐
- Java实现将zip文件解压
- java.util.zip对zip文件解压
- java实现将ZIP压缩文件解压的工具类
- java.util.zip 的解压和压缩文件夹
- java解压7-zip压缩文件
- 在Java代码中使用ZipFile解压zip文件
- 原生java 压缩解压zip文件
- java 解压zip、rar文件资料收集
- java csv文件放入zip 解压以后乱码
- java.util.zip包无法正常解压rar软件压缩的rar文件
- java 解压本地zip压缩文件
- java解压zip、rar(多级文件)
- java中不解压读取zip压缩文件
- java压缩及解压(.zip)文件文件夹
- Java文件压缩与解压之java.util.zip的使用
- java解压zip文件示例
- 在 Java 中解压 zip 文件
- java 解压zip压缩文件
- Java解压和压缩带密码的zip文件
- Java zip,rar解压中文文件,以及包含在文件夹中的中文文件