像下面一个这样的程序,在2.zip里有个2.txt的文本文件,里面的文本量很大就像下面那样解压出来,输出到ok.txt,这两个文件的大小是一样的,但是ok.txt只有2.txt的前一点内容,还不到10%,剩下的全是空格,是空格,不是空白!可以用光标定位!求解决!
public static void zipTest() throws IOException{
ZipFile zipFile = new ZipFile("d:\\2.zip");
InputStream is = zipFile.getInputStream(zipFile.getEntry("2.txt"));
byte [] b = new byte[is.available()];
is.read(b);
is.close();

FileOutputStream fo = new FileOutputStream("D://ok.txt");
fo.write(b);
fo.flush();
fo.close();
}

解决方案 »

  1.   

    读的方式不对,
    javadoc文档说的:Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer.应该循环读,直到读不出为止。另外,楼主应该用ultra edit之类的工具看看这个文件,看文件中的字节到底是00(空白)还是32(空格)。
      

  2.   


    我修改用FileInputStream代替,这样的读取并没有任何问题
    public static void zipTest() throws IOException{
    /*
    ZipFile zipFile = new ZipFile("d:/2.zip");
    InputStream is = zipFile.getInputStream(zipFile.getEntry("2.txt"));
    */
    FileInputStream is = new FileInputStream("d:/2.txt");
    byte [] b = new byte[is.available()];
    is.read(b);
    is.close();

    FileOutputStream fo = new FileOutputStream("D:/ok.txt");
    fo.write(b);
    fo.flush();
    fo.close();
    }
      

  3.   


    public static void zipTest() throws IOException{
            ZipFile zipFile = new ZipFile("d:\\2.zip");
            InputStream is = zipFile.getInputStream(zipFile.getEntry("2.txt"));
            byte [] b = new byte[is.available()];
            is.read(b); //是不是jvm虚拟机内的缓存大小默认有限制,2.txt的数据超过大小,所以无法全部读取呢?
            is.close();
            
            FileOutputStream fo = new FileOutputStream("D://ok.txt");
            fo.write(b);
            fo.flush(); //OutputStream应该不需要使用刷新吧?
            fo.close();
        }//循环读取
    public static void zipTest() throws IOException{
            ZipFile zipFile = new ZipFile("d:\\2.zip");
            InputStream is = zipFile.getInputStream(zipFile.getEntry("2.txt"));
            FileOutputStream fo = new FileOutputStream("D://ok.txt");        //byte [] b = new byte[is.available()];
            byte [] b = new byte[1024*10]; //1024*10,可适当调整
            int len = 0;        while((len=is.read())!=-1)
            {
                  fo.write(b,0,len)  
            }
            
            is.close();
            fo.close();
        }
      

  4.   

    是的,FileInputStream一般是没有问题的。
    网络等其他场合都有问题的。
      

  5.   

    你后面一句就是fo.close();
    这种情况下fo.flush();可以不做的。
      

  6.   

    用了3L那段代码,文本变成了71M!本来才626KB的
    记事本无响应,eclipse错误,写字板读取很慢,显示出来是一堆空白,刚下了个Notepad++,显示了一堆NULL
      

  7.   

    抱歉,没仔细看,以为3楼测试过的。
    3楼的while循环
    应该这么写:         while((len=is.read(b))!=-1)
            {
                  fo.write(b,0,len);
            }
      

  8.   

    之所以ok.txt出问题是因为的从2.txt读取数据到byte[]b出了问题,建议还是用循环读取
    代码可以在三楼的基础上再写入zipFile
      

  9.   

    API说的很清楚了
    Note that while some implementations of InputStream will return
    the total number of bytes in the stream, many will not.  It is
     never correct to use the return value of this method to allocate
    a buffer intended to hold all data in this stream.注意,有些 InputStream 的实现将返回流中的字节总数,但也有很多实现不会这样做。试图使用此方法的返回值分配缓冲区,以保存此流所有数据的做法是不正确的