怎么样解析ZIP(JAVA)

wjch93734 2006-05-31 06:14:52
JAVA怎么样解压缩ZIP文件.急
...全文
373 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cooler 2006-06-14
  • 打赏
  • 举报
回复
public class Zip {

public void zip(InputStream in, OutputStream out) throws IOException {
GZIPOutputStream zip = new GZIPOutputStream(out);
try {
byte[] buffer = new byte[1024];
int bytesRead;
while ( (bytesRead = in.read(buffer)) != -1) {
zip.write(buffer, 0, bytesRead);
}
zip.finish();
}
finally {
if (zip != null) {
try {
zip.close();
}
catch (Throwable e) {}
}
}
}

public void zip(File f, File fout) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(f);
out = new FileOutputStream(fout);
zip(in, out);
in.close();
in = null;
boolean deleted;
try {
deleted = f.delete();
}
catch (Throwable e) {
deleted = false;
}
if (!deleted)
f.deleteOnExit();
}
catch (IOException e) {
if (out != null)
try {
out.close();
out = null;
}
catch (Throwable ex) {}
if (fout != null) {
boolean deleted;
try {
deleted = fout.delete();
}
catch (Throwable ex) {
deleted = false;
}
if (!deleted)
fout.deleteOnExit();
}
}
finally {
if (in != null)
try {
in.close();
}
catch (Throwable e) {}
if (out != null)
try {
out.close();
}
catch (Throwable e) {}
}
}

public void zip(File fin, String filename) throws IOException {
zip(fin, new File(filename));
}

public void zip(String fin, String fout) throws IOException {
zip(new File(fin), new File(fout));
}

public void zip(String fin, File fout) throws IOException {
zip(new File(fin), fout);
}

public void zip(File f) throws IOException {
String path = getDirname(f.getPath());
String name = getBasename(f.getName());
File dir = new File(path);
File fout = new File(dir, name + ".zip");
zip(f, fout);
}

public String getDirname(String path) {
char sep = File.separatorChar;
int pos1 = path.lastIndexOf(sep);
int pos2 = path.lastIndexOf('/');
int pos = pos1;
if (pos1 < pos2)
pos = pos2;
if (pos > 0)
return path.substring(0, pos);
return ".";
}

public String getBasename(String path) {
char sep = File.separatorChar;
int pos1 = path.lastIndexOf(sep);
int pos2 = path.lastIndexOf('/');
int pos = pos1;
if (pos1 < pos2)
pos = pos2;
if (pos > 0)
return path.substring(pos + 1);
return path;
}

public void zip(String fname) throws IOException {
zip(new File(fname));
}

public void unzip(InputStream in, OutputStream out) throws IOException {
GZIPInputStream unzip = new GZIPInputStream(in);
try {
byte[] buffer = new byte[1024];
int bytesRead;
while ( (bytesRead = unzip.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
finally {
if (unzip != null) {
try {
unzip.close();
}
catch (Throwable e) {}
}
}
}

public void unzip(File fin, File fout) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(fin);
out = new FileOutputStream(fout);
unzip(in, out);
in.close();
in = null;
boolean deleted;
try {
deleted = fin.delete();
}
catch (Throwable e) {
deleted = false;
}
if (!deleted)
fin.deleteOnExit();
}
catch (IOException e) {
if (out != null)
try {
out.close();
out = null;
}
catch (Throwable ex) {}
if (fout != null) {
boolean deleted;
try {
deleted = fout.delete();
}
catch (Throwable ex) {
deleted = false;
}
if (!deleted)
fout.deleteOnExit();
}
}
finally {
if (in != null)
try {
in.close();
}
catch (Throwable e) {}
if (out != null)
try {
out.close();
}
catch (Throwable e) {}
}
}

public void unzip(File fin, String filename) throws IOException {
unzip(fin, new File(filename));
}

public void unzip(String fin, String fout) throws IOException {
unzip(new File(fin), new File(fout));
}

public void unzip(String fin, File fout) throws IOException {
unzip(new File(fin), fout);
}

/**
* 解压单个文件的压缩包,请注意如果压缩包有多个文件,不会成功也不会报错
* @param f
* @throws IOException
*/
public void unzip(File f) throws IOException {
String path = getDirname(f.getPath());
String name = getBasename(f.getName());
int len = name.length();
File dir = new File(path);
File fout = new File(dir, name.substring(0, len - 4));
unzip(f, fout);
}

/**
* 解压单个文件的压缩包,请注意如果压缩包有多个文件,不会成功也不会报错
* @param fname
* @throws IOException
*/
public void unzip(String fname) throws IOException {
unzip(new File(fname));
}

/**
* 解压含有多个文件的ZIP包
* @param zipFileName
* @param extPlace
*/
private static void extZipFileList(String zipFileName, String extPlace) {
try {

ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName));

ZipEntry entry = null;

while ( (entry = in.getNextEntry()) != null) {

String entryName = entry.getName();

if (entry.isDirectory()) {
File file = new File(extPlace + entryName);
file.mkdirs();
System.out.println("创建文件夹:" + entryName);
}
else {

FileOutputStream os = new FileOutputStream(extPlace
+ entryName);

// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];

int len;
while ( (len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry();

}
}

}
catch (IOException e) {
e.printStackTrace();
}
System.out.println("解压文件成功");
}

public static void main(String[] args) throws IOException {
Zip zip = new Zip();
zip.zip("c:\\111.xls");
zip.extZipFileList("c:\\22.zip","c:\\");
}
}
tianfang 2006-06-01
  • 打赏
  • 举报
回复
说的不清楚
1、你怎样的压缩下载的(怎么实现的,问题可能在这里)
2、server端、client端都是你做的吗
wjch93734 2006-06-01
  • 打赏
  • 举报
回复
我用的是java.util.zip包,
我已经实现了下载压缩(里面是XML文件).
但是压缩完之后java.util.zip把这个XML文件里面的标签和内容顺序给打乱了.

有没有办法下载 后并压缩成ZIP文件后,还保持原来的XML顺序和内容....
因为我导入的时候先解压缩这个ZIP文件.但是循序和内容给打乱了.导入就出问题了.
wjch93734 2006-06-01
  • 打赏
  • 举报
回复
我用的是java.util.zip包,
我已经实现了下载压缩(里面是XML文件).
但是压缩完之后java.util.zip把这个XML文件里面的标签和内容顺序给打乱了.

有没有办法下载 后并压缩成ZIP文件后,还保持原来的XML顺序和内容....
因为我导入的时候先解压缩这个ZIP文件.但是循序和内容给打乱了.导入就出问题了.
yingge 2006-05-31
  • 打赏
  • 举报
回复
java.util.zip

用这个包试试

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧