ZipInputStream的问题 散200分~~~~

insiku 2007-04-25 09:55:09
解压zip文件 不含中文

但是 用ZipInputStream解压出来的文件跟源文件比较有差异
(表现为 某些地方会添加0x20 即空格 文件尾缺少 原以为是缓冲的问题 不停的flush也没用)

使用ZipFile的正确

想问问是什么原因 那些0x20是怎么多出来的

代码如下

使用ZipInputStream解压 (解压不正确)

public class ZipTest2
{
private static int length = 9;
public static void main(String[] args) throws IOException
{
File f1 = new File("C:\\test.zip");

ZipInputStream zin = new ZipInputStream(new FileInputStream(f1));

ZipEntry zinentry;

while((zinentry = zin.getNextEntry()) != null)
{
File f = new File("C:\\" + zinentry.getName());
if(!f.exists())
{
if(zinentry.isDirectory())
f.mkdirs();
else
f.createNewFile();
}
if(zinentry.isDirectory())
{
zin.closeEntry();
continue;
}
FileOutputStream output = new FileOutputStream(f);
long size = zinentry.getSize();
while(size >= length)
{
System.out.println(size);
byte[] bytes = new byte[length];
zin.read(bytes,0 , length);
output.write(bytes, 0, length);
// output.flush();
size -= length;
}

if(size > 0 && size < length)
{
System.out.println(size);
byte[] bytes = new byte[(int)size];
zin.read(bytes, 0, (int)size);
output.write(bytes, 0, (int)size);
// output.flush();
}
output.close();

zin.closeEntry();

}

zin.close();
}
}




使用ZipFile解压 (解压正确)

public class ZipTest3
{
private static int length = 2048;

public static void main(String[] args) throws IOException
{
File f = new File("C:\\test.zip");
ZipFile zipf = new ZipFile(f);

Enumeration zipEntrys = zipf.entries();

while (zipEntrys.hasMoreElements())
{
ZipEntry zipEntry = (ZipEntry) zipEntrys.nextElement();
File tempf = new File("C:\\" + zipEntry.getName());

if (!tempf.exists())
{
if (zipEntry.isDirectory())
tempf.mkdirs();
else
tempf.createNewFile();
}

InputStream input = zipf.getInputStream(zipEntry);
FileOutputStream output = new FileOutputStream(tempf);
long size = zipEntry.getSize();

while(size >= length)
{
byte[] bytes = new byte[length];
input.read(bytes);
output.write(bytes);
// output.flush();

size -= length;
}

if(size > 0 && size < length)
{
byte[] bytes = new byte[(int)size];
input.read(bytes);
output.write(bytes);
// output.flush();
}
input.close();
output.close();

}

zipf.close();
}
}
...全文
395 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyunai520 2010-04-15
  • 打赏
  • 举报
回复
问题说出来啊 ````我遇到和你一样的问题
insiku 2007-04-25
  • 打赏
  • 举报
回复
问题找到了 结帖
insiku 2007-04-25
  • 打赏
  • 举报
回复
我将length 设置为1024

一下是每次read的实际读取长度

1024
459 //这次459就不在预料中 也就是在理论上 流中剩下的字节大于需要读取的长度时 依然会出现只读一部分 在非压缩文件中我没遇到过
1024
1024
1024
1024
479 //这次479是预料的 因为不一定刚好是1024的倍数
insiku 2007-04-25
  • 打赏
  • 举报
回复
但是 实际上产生的文件 跟源文件length是相同的
只是中间会多出0x20 然后结尾少

然后我在程序里面判断是否够我设定的长度

while(size >= length)
{
...
}

if(size > 0 && size < length)
{
...
}

所以说不是最后少一段 是中间因为read并没有读取足够 但是并不是在文件末尾
奇怪的问题
ChDw 2007-04-25
  • 打赏
  • 举报
回复
但是并不是因为没有可读取的字节 为什么会出现这种情况???
-----------------
流中有可读的字节,但是read(buff)时,你的buff可能是1024长,但是流中只有10个字节,那么buff就只有前10个字节有用,后面全部是无用的

你write的时候就必须正确的指定长度,不要将后面错误的字节写到流中
ChDw 2007-04-25
  • 打赏
  • 举报
回复
如果刚好除得尽就没有问题啦,你两个程序中的length不等
angelleecash 2007-04-25
  • 打赏
  • 举报
回复
同意楼上说法,用循环拉
比如
int totalLength = getTotalLengthInSomeWay();
int lengthRead = 0;
while(lengthRead<totalLength){
lengthRead += zin.read(bytes,lengthRead,totalLength-lengthRead);
}
insiku 2007-04-25
  • 打赏
  • 举报
回复
to ChDw(米)

确实如此
但是并不是因为没有可读取的字节 为什么会出现这种情况???
使用bufferedinputStream 就不会出现 这又是为什么?

ChDw 2007-04-25
  • 打赏
  • 举报
回复
你的代码可以说都写得有点问题
应该是
int readed = zin.read(bytes,0 , length);
output.write(bytes, 0, readed);//read方法并不保证它全部read到你指定的length


int readed = input.read(bytes);
output.write(bytes, 0, readed);
zunshanke2004 2007-04-25
  • 打赏
  • 举报
回复
帮顶
dovelee 2007-04-25
  • 打赏
  • 举报
回复
ding
suxq126 2007-04-25
  • 打赏
  • 举报
回复
帮顶
约翰羊 2007-04-25
  • 打赏
  • 举报
回复
帮顶
frilly 2007-04-25
  • 打赏
  • 举报
回复
再加个修饰类呢??例如: BufferedInputStream,BufferedOutputStream
在这个基础上再用ZipInputStream 不知道如何??
9441 2007-04-25
  • 打赏
  • 举报
回复
不懂,帮顶
insiku 2007-04-25
  • 打赏
  • 举报
回复
200分 每人来领??????????????
基于GUI用IO流中的ZipOutputStream ,ZipInputStream实现文件的解压与压缩, 如文件解压函数如下 private void unZip(String zipFileName, String outputDirectory)throws Exception { InputStream in=null; FileOutputStream out=null; try { ZipFile zipFile = new ZipFile(zipFileName); java.util.Enumeration e = zipFile.getEntries(); ZipEntry zipEntry = null; createDirectory(outputDirectory, ""); while (e.hasMoreElements()) { zipEntry = (ZipEntry) e.nextElement(); System.out.println("正在解压: " + zipEntry.getName()); String name= null; if (zipEntry.isDirectory()) { name = zipEntry.getName(); name = name.substring(0, name.length() - 1); File f = new File(outputDirectory + File.separator + name); f.mkdir(); System.out.println("创建目录:" + outputDirectory + File.separator + name); } else { String fileName = zipEntry.getName(); fileName = fileName.replace('\\', '/'); if (fileName.indexOf("/") != -1) { createDirectory(outputDirectory, fileName.substring(0,fileName.lastIndexOf("/"))); fileName = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length()); } File f = new File(outputDirectory + File.separator + zipEntry.getName()); f.createNewFile(); in = zipFile.getInputStream(zipEntry); out = new FileOutputStream(f); byte[] by = new byte[1024]; int c; while ((c = in.read(by)) != -1) { out.write(by, 0, c); } out.flush(); } } } catch (Exception ex) { System.out.println(ex.getMessage()); }finally{ out.close(); in.close(); } }

62,616

社区成员

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

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