ZipInputStream的问题 散200分~~~~
解压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();
}
}