现在那个流的构造应该没问题的吧,我实际用的时候是这样的: in = new DataInputStream(new BufferedInputStream(zipIN));现在的问题是:为什么我读文件的时候,读的结果是文件是空的??并不是读到乱码,连用zipIN.read()读都得到的是-1????
现确定你的GZIP文件是不是标准文件,可以用java自带的GZIP压缩一个文件试一试。
/** * <p>Title: 解压文件处理 </p> * <p>Description: 实现与解压文件的相关的处理 </p> * <p>Copyright: Copyright (c) 2003-2010</p> * @version 1.0 03/08/12 */import java.io.*; import java.util.*; import java.util.zip.*; import sun.io.*;public class Zip { public String errMsg = ""; public Zip() { } /** * 解压文件 * @param filePath 文件路径 * @param fileName 文件名称 * @return 错误信息字符串 */ public String execute(String filePath, String fileName) { File infile = new File(filePath + fileName); 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); } else { dirname = infile.getAbsolutePath(); } ///JOptionPane.showMessageDialog(null, "1. " + dirname, "显示", JOptionPane.CLOSED_OPTION); File newdir = new File(dirname); newdir.mkdir();//创建第一级目录 byte[] c = new byte[1024]; int len; int slen; while (file != null) { i = file.getName().replace('/', '\\').lastIndexOf('\\'); if (i != -1) { File dirs = new File(dirname + File.separator + file.getName().replace('/', '\\')); dirs.mkdir(); dirs = null; ///JOptionPane.showMessageDialog(null, "2. " + String.valueOf(i) + dirname + File.separator + file.getName().replace('/', '\\').toString(), "显示", JOptionPane.CLOSED_OPTION); //System.exit(0); } if (file.isDirectory()) { File dirs = new File(dirname + File.separator + file.getName().replace('/', '\\')); dirs.mkdir(); dirs = null; ///JOptionPane.showMessageDialog(null, "3. " + dirname + File.separator + file.getName().replace('/', '\\').toString(), "显示", JOptionPane.CLOSED_OPTION); } else { FileOutputStream out = new FileOutputStream(dirname + File.separator + file.getName().replace('/', '\\')); while ((slen = in.read(c, 0, c.length)) != -1) { out.write(c, 0, slen); } out.close(); } file = in.getNextEntry(); } errMsg = infile.getName() + "解压成功!"; in.close(); return errMsg; } catch (ZipException zipEx) { errMsg = infile.getName() + "不是一个zip文件!"; return errMsg; } catch (IOException ioEx) { errMsg = "读取" + infile.getName() + "文件时出错!"; return errMsg; } catch (Exception Ex) { errMsg = infile.getName() + "文件不存在!"; return errMsg; } } }http://xieweibbs.topcities.com
TO: coolarmy(bb考拉猪) 我放在服务器上的压缩文件就是用Zip或者GZIP的outputStream压缩放的,我用另外写的解压的JAVA文件能解得开的,那个解压文件的写法应该就和楼上贴的那个差不多了.用到了ZipFile zip = new ZipFile(infile);的,用那种方式来解是没问题的,所以应该压缩文件是没问题的吧,但是我现在是存在服务器上的文件,只能以流的方式来读,不能构造ZipFile,所以不能用那种解压的方式了.大家帮帮忙啊,急啊.
in = new DataInputStream(new BufferedInputStream(zipIN));现在的问题是:为什么我读文件的时候,读的结果是文件是空的??并不是读到乱码,连用zipIN.read()读都得到的是-1????
* <p>Title: 解压文件处理 </p>
* <p>Description: 实现与解压文件的相关的处理 </p>
* <p>Copyright: Copyright (c) 2003-2010</p>
* @version 1.0 03/08/12
*/import java.io.*;
import java.util.*;
import java.util.zip.*;
import sun.io.*;public class Zip {
public String errMsg = ""; public Zip() {
} /**
* 解压文件
* @param filePath 文件路径
* @param fileName 文件名称
* @return 错误信息字符串
*/
public String execute(String filePath, String fileName) {
File infile = new File(filePath + fileName);
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);
} else {
dirname = infile.getAbsolutePath();
}
///JOptionPane.showMessageDialog(null, "1. " + dirname, "显示", JOptionPane.CLOSED_OPTION);
File newdir = new File(dirname);
newdir.mkdir();//创建第一级目录 byte[] c = new byte[1024];
int len;
int slen; while (file != null) {
i = file.getName().replace('/', '\\').lastIndexOf('\\');
if (i != -1) {
File dirs = new File(dirname + File.separator + file.getName().replace('/', '\\'));
dirs.mkdir();
dirs = null;
///JOptionPane.showMessageDialog(null, "2. " + String.valueOf(i) + dirname + File.separator + file.getName().replace('/', '\\').toString(), "显示", JOptionPane.CLOSED_OPTION);
//System.exit(0);
} if (file.isDirectory()) {
File dirs = new File(dirname + File.separator + file.getName().replace('/', '\\'));
dirs.mkdir();
dirs = null;
///JOptionPane.showMessageDialog(null, "3. " + dirname + File.separator + file.getName().replace('/', '\\').toString(), "显示", JOptionPane.CLOSED_OPTION);
} else {
FileOutputStream out = new FileOutputStream(dirname + File.separator + file.getName().replace('/', '\\'));
while ((slen = in.read(c, 0, c.length)) != -1) {
out.write(c, 0, slen);
}
out.close();
} file = in.getNextEntry(); }
errMsg = infile.getName() + "解压成功!";
in.close();
return errMsg; } catch (ZipException zipEx) {
errMsg = infile.getName() + "不是一个zip文件!";
return errMsg;
} catch (IOException ioEx) {
errMsg = "读取" + infile.getName() + "文件时出错!";
return errMsg;
} catch (Exception Ex) {
errMsg = infile.getName() + "文件不存在!";
return errMsg;
}
}
}http://xieweibbs.topcities.com
ZipInputStream zipIN = null;
DataInputStream in = null;
URL url = null;
try{
url = new URL("http://localhost/top.zip");
InputStream is=url.openStream();
//
//
zipIN = new ZipInputStream(is);
ZipEntry file = zipIN.getNextEntry();
System.out.println("n=========="+File.getName());
in = new DataInputStream(zipIN);
String s = in.readLine();
System.out.println("s=========="+s);
}catch(Exception e)
{
System.out.println("Err=" + e);
}