解压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();
    }
}

解决方案 »

  1.   

    再加个修饰类呢??例如: BufferedInputStream,BufferedOutputStream
    在这个基础上再用ZipInputStream 不知道如何??
      

  2.   

    你的代码可以说都写得有点问题
    应该是
    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);
      

  3.   

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

  4.   

    同意楼上说法,用循环拉
    比如
    int totalLength = getTotalLengthInSomeWay();
    int lengthRead = 0;
    while(lengthRead<totalLength){
    lengthRead += zin.read(bytes,lengthRead,totalLength-lengthRead);
    }
      

  5.   

    如果刚好除得尽就没有问题啦,你两个程序中的length不等
      

  6.   

    但是并不是因为没有可读取的字节   为什么会出现这种情况???
    -----------------
    流中有可读的字节,但是read(buff)时,你的buff可能是1024长,但是流中只有10个字节,那么buff就只有前10个字节有用,后面全部是无用的你write的时候就必须正确的指定长度,不要将后面错误的字节写到流中
      

  7.   

    但是 实际上产生的文件 跟源文件length是相同的
    只是中间会多出0x20  然后结尾少 然后我在程序里面判断是否够我设定的长度            while(size >= length)
                {
    ...
                }
                
                if(size > 0 && size < length)
                {
    ...
                }所以说不是最后少一段  是中间因为read并没有读取足够  但是并不是在文件末尾  
    奇怪的问题
      

  8.   

    我将length 设置为1024一下是每次read的实际读取长度1024
    459                //这次459就不在预料中  也就是在理论上 流中剩下的字节大于需要读取的长度时 依然会出现只读一部分  在非压缩文件中我没遇到过
    1024
    1024
    1024
    1024
    479                //这次479是预料的 因为不一定刚好是1024的倍数