Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java将文件从Zip文件读取到内存?_Java_Zip_Unzip - Fatal编程技术网

如何使用Java将文件从Zip文件读取到内存?

如何使用Java将文件从Zip文件读取到内存?,java,zip,unzip,Java,Zip,Unzip,我在SUN网站上找到了一个例子(http://java.sun.com/developer/technicalArticles/Programming/compression/),但它返回BufferedOutputStream。但我希望将ZipEntry文件作为InputStream,然后处理下一个文件。可能吗?我的程序无法访问硬盘,所以它甚至不能临时保存文件 import java.io.*; import java.util.zip.*; public class UnZip {

我在SUN网站上找到了一个例子(http://java.sun.com/developer/technicalArticles/Programming/compression/),但它返回BufferedOutputStream。但我希望将ZipEntry文件作为InputStream,然后处理下一个文件。可能吗?我的程序无法访问硬盘,所以它甚至不能临时保存文件

import java.io.*;
import java.util.zip.*;

public class UnZip {
   final int BUFFER = 2048;
   public static void main (String argv[]) {
      try {
         BufferedOutputStream dest = null;
         FileInputStream fis = new 
       FileInputStream(argv[0]);
         ZipInputStream zis = new 
       ZipInputStream(new BufferedInputStream(fis));
         ZipEntry entry;
         while((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " +entry);
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new 
          FileOutputStream(entry.getName());
            dest = new 
              BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) 
              != -1) {
               dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
         }
         zis.close();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}

好的,只需更改将文件写入数据的部分

while((entry=zis.getnextery())!=null){
System.out.println(“提取:+输入”);
整数计数;
字节[]数据=新字节[缓冲区];
字符串文件名=entry.getName();
System.out.println(“文件名:“+Filename”);
而((计数=zis.read(数据,0,缓冲区))!=-1){
//对数据变量执行任何操作
系统输出打印项次(数据);
}
}

字节[]数据=新字节[缓冲区]
??@JohnSmith在这里加上1024个int,这是每次迭代时从zip条目中读取的字节数如果要将
数据
转换为字符串,请不要忘记删除数组中的尾随空字节!将其转换为新字符串(数据)。replaceAll(“\u0000.*”,”)。此解决方案来自@Jay no,您从
数据中提取
计数
字节,以首先防止这些空字符!无论如何,谢谢你的编辑。对,很有意义,谢谢你的澄清!所以应该是
新字符串(data,0,count)
哪个更好:)