GVKun编程网logo

是否可以从zipinputstream获取zipentry的输入流?(zipinputstream.read方法)

9

在本文中,我们将带你了解是否可以从zipinputstream获取zipentry的输入流?在这篇文章中,我们将为您详细介绍是否可以从zipinputstream获取zipentry的输入流?的方方面

在本文中,我们将带你了解是否可以从zipinputstream获取zipentry的输入流?在这篇文章中,我们将为您详细介绍是否可以从zipinputstream获取zipentry的输入流?的方方面面,并解答zipinputstream.read方法常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的GZIPInputStream转换为String、GZIPInputStream逐行读取、GZIPOutputStream GZIPInputStream、IO流之ZipInputStream和ZipOutputStream的认识及使用

本文目录一览:

是否可以从zipinputstream获取zipentry的输入流?(zipinputstream.read方法)

是否可以从zipinputstream获取zipentry的输入流?(zipinputstream.read方法)

我正在从另一个来源接收ZipInputStream,并且需要将第一项的InputStream提供给另一个来源。

我希望能够在不将临时文件保存在设备上的情况下执行此操作,但是,我知道获取单个条目的InputStream的唯一方法是通过ZipFile.getInputStream(entry),并且因为我有一个ZipInputStream而不是ZipFile
, 这是不可能的。

所以我最好的解决方案是

  1. 将输入的InputStream保存到文件
  2. 读取文件为ZipFile
  3. 使用第一个条目的InputStream
  4. 删除临时文件。

答案1

小编典典

想通了:

完全有可能,在条目开始处ZipInputStream.getNextEntry()定位位置InputStream并因此提供的调用ZipInputStream等同于提供ZipEntryInputStream

ZipInputStream是足够聪明来处理条目的EOF下游,或者看起来是这样。

p。

GZIPInputStream转换为String

GZIPInputStream转换为String

首先,对不起,如果我的术语有点业余,请尝试忍受我;)

我正在尝试将HTTP响应的压缩后的正文转换为纯文本。我已将此响应的字节数组转换为ByteArrayInputStream。然后,我将其转换为GZIPInputStream。现在,我想读取GZIPInputStream并将最终解压缩的HTTP响应正文存储为纯文本字符串。

这段代码会将最终解压缩的内容存储在OutputStream中,但我想将内容存储为String:

public static int sChunk = 8192;
ByteArrayInputStream bais = new ByteArrayInputStream(responseBytes);
GZIPInputStream gzis = new GZIPInputStream(bais);
byte[] buffer = new byte[sChunk];
int length;
while ((length = gzis.read(buffer,sChunk)) != -1) {
        out.write(buffer,length);
}

GZIPInputStream逐行读取

GZIPInputStream逐行读取

我有一个.gz格式的文件。读取此文件的Java类是GZIPInputStream。但是,此类不会扩展Java的BufferedReader类。结果,我无法逐行读取文件。我需要这样的东西

reader  = new MyGZInputStream( some constructor of GZInputStream) reader.readLine()...

尽管我创建了扩展java的Reader或BufferedReader类并使用GZIPInputStream作为其变量之一的类。

import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.Reader;import java.util.zip.GZIPInputStream;public class MyGZFilReader extends Reader {    private GZIPInputStream gzipInputStream = null;    char[] buf = new char[1024];    @Override    public void close() throws IOException {        gzipInputStream.close();    }    public MyGZFilReader(String filename)               throws FileNotFoundException, IOException {        gzipInputStream = new GZIPInputStream(new FileInputStream(filename));    }    @Override    public int read(char[] cbuf, int off, int len) throws IOException {        // TODO Auto-generated method stub        return gzipInputStream.read((byte[])buf, off, len);    }}

但是,这在我使用时不起作用

BufferedReader in = new BufferedReader(    new MyGZFilReader("F:/gawiki-20090614-stub-meta-history.xml.gz"));System.out.println(in.readLine());

有人可以建议如何进行..

答案1

小编典典

装饰器的基本设置如下:

InputStream fileStream = new FileInputStream(filename);InputStream gzipStream = new GZIPInputStream(fileStream);Reader decoder = new InputStreamReader(gzipStream, encoding);BufferedReader buffered = new BufferedReader(decoder);

此代码段中的关键问题是的值encoding。这是文件中文本的字符编码。是“ US-ASCII”,“ UTF-8”,“ SHIFT-JIS”,“
ISO-8859-9”等吗?有数百种可能性,通常无法从文件本身确定正确的选择。必须通过一些带外通道来指定。

例如,也许这是平台默认值。但是,在网络环境中,这非常脆弱。写入文件的计算机可能位于相邻的小隔间中,但是具有不同的默认文件编码。

大多数网络协议使用标头或其他元数据来显式记录字符编码。

在这种情况下,从文件扩展名看来,内容是XML。为此,XML在XML声明中包含“
encoding”属性。此外,应该真正使用XML解析器而不是文本来处理XML。逐行读取XML似乎是一种脆弱的特殊情况。

未能明确指定编码违反第二条诫命。 使用默认编码会带来麻烦!

GZIPOutputStream GZIPInputStream

GZIPOutputStream GZIPInputStream

GZIP is appropriate for single data stream.

Example: Compress one file

 

public class Demo8 {
 
 public static void main(String[] args) throws Exception {
  //read a file
  FileReader fr = new FileReader("C:/Users/caich5/Desktop/aaaa.txt");
  
        BufferedReader in = new BufferedReader(fr);
       
        //starting to compress
        FileOutputStream fos = new FileOutputStream("C:/Users/caich5/Desktop/aaaa.gz");
        GZIPOutputStream gzipout = new GZIPOutputStream(fos);
        BufferedOutputStream bos = new BufferedOutputStream(gzipout);
       

        int c;
        while((c = in.read())!= -1){
         bos.write(c);
        }
        in.close();
        bos.close();
        
        //reading  gzip file 
        FileInputStream fis = new FileInputStream("C:/Users/caich5/Desktop/aaaa.gz");
        GZIPInputStream gzipin = new GZIPInputStream(fis);
        InputStreamReader isr = new InputStreamReader(gzipin);
        BufferedReader in2 = new BufferedReader(isr);
       
        String s;
        while((s = in2.readLine())!= null){
         System.out.println(s);
         
        }
 }
}

IO流之ZipInputStream和ZipOutputStream的认识及使用

IO流之ZipInputStream和ZipOutputStream的认识及使用

转载https://blog.csdn.net/weixin_39723544/article/details/80611810

更多内容 转载https://blog.csdn.net/justry_deng/article/details/82846356

工具类

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

public class ZipUtils {
/**
* Default buff byte size
*
*/
private static final int DEFAULT_BUFF_SIZE = 1024;

/**
* Default basedir value
*
*/
private static final boolean DEFAULT_DIR = false;

public static void decompress(String srcPath) throws Exception {
decompress(new File(srcPath));
}

public static void decompress(File srcFile) throws Exception {
File baseFile = srcFile.getParentFile();
decompress(srcFile, baseFile);
}

public static void decompress(String srcPath, String destPath) throws Exception {
decompress(new File(srcPath), new File(destPath));
}

public static void decompress(File srcFile, File destFile) throws Exception {
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis);
doDecompress(destFile, zis);
zis.close();
}

private static void doDecompress(File destFile, ZipInputStream zis) throws Exception {
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
String dir = destFile.getPath() + File.separator + zipEntry.getName();
File dirFile = new File(dir);
fileProber(dirFile);
if (zipEntry.isDirectory()) {
dirFile.mkdirs();
} else {
doDecompressFile(dirFile, zis);
}
zis.closeEntry();
}
}

private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int len;
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) {
bos.write(buff, 0, len);
}
bos.close();
}

/**
* 文件探测
*
* When the parent file not exist.Create it.
*
* @param dirFile
* @throws Exception
*/
public static void fileProber(File dirFile) throws Exception {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
fileProber(parentFile);
parentFile.mkdirs();
}
}

public static void compress(String srcPath, String destPath,boolean dirFlag) throws Exception {
compress(new File(srcPath), new File(destPath), dirFlag);
}

public static void compress(String srcPath, String destPath) throws Exception {
compress(new File(srcPath), new File(destPath), DEFAULT_DIR);
}

public static void compress(File srcFile, File destFile, boolean dirFlag) throws Exception {
compress(srcFile, new ZipOutputStream(new FileOutputStream(destFile)), dirFlag);
}

public static void compress(File srcFile, ZipOutputStream zos, boolean dirFlag) throws Exception {
if (srcFile.isDirectory()) {
if (dirFlag) {
doCompress(zos, srcFile, srcFile.getName() + File.separator);
} else {
doCompress(zos, srcFile, "");
}
} else {
doCompress(zos, srcFile, "");
}
zos.close();
}

public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
doCompress(zos, files[i], baseDir);
}
} else {
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
In
putStream in = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));
int len;
while ((len = in.read(buff,0 ,DEFAULT_BUFF_SIZE)) != -1) {
zos.write(buff, 0, len);
}
in.close();
}
}
}
}

压缩

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class condense {
private static final boolean DEFAULT_DIR = true;
private static final int DEFAULT_BUFF_SIZE = 1024;

// 压缩入口1
public static void compress(String srcPath, String destPath,boolean dirFlag) throws Exception {
compress(new File(srcPath), new File(destPath), dirFlag);
}

// 压缩入口2
public static void compress(String srcPath, String destPath) throws Exception {
compress(new File(srcPath), new File(destPath), DEFAULT_DIR);
}

// 压缩入口3
public static void compress(File srcFile, File destFile, boolean dirFlag) throws Exception {
compress(srcFile, new ZipOutputStream(new FileOutputStream(destFile)), dirFlag);
}

public static void compress(File srcFile, ZipOutputStream zos, boolean dirFlag) throws Exception {
// 需要解压的压缩文件对象
// 压缩输出流
// 是否在压缩文件时创建一个父文件夹后再压缩
if (srcFile.isDirectory()) {
if (dirFlag) {
doCompress(zos, srcFile, srcFile.getName() + File.separator);
} else {
doCompress(zos, srcFile, "");
}
} else {
doCompress(zos, srcFile, "");
}
zos.close();
}

public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {
if (file.isDirectory()) {
// 递归循环,只压缩其中所有文件
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
doCompress(zos, files[i], baseDir);
}
} else {
// 进行文件压缩的操作
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
InputStream in = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));
int len;
while ((len = in.read(buff,0 ,DEFAULT_BUFF_SIZE)) != -1) {
zos.write(buff, 0, len);
}
in.close();
}
}
}
解压
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class decompress {
/**
* Default buff byte size
*/
private static final int DEFAULT_BUFF_SIZE = 1024;

// 程序入口1
public static void decompress(String srcPath) throws Exception {
decompress(new File(srcPath));
}

// 程序入口2
public static void decompress(File srcFile) throws Exception {
File baseFile = srcFile.getParentFile();
decompress(srcFile, baseFile);
}

// 程序入口3
public static void decompress(String srcPath, String destPath) throws Exception {
decompress(new File(srcPath), new File(destPath));
}

// 程序基本入口
public static void decompress(File srcFile, File destFile) throws Exception {
CheckedInputStream cis = new CheckedInputStream(new FileInputStream(srcFile), new CRC32());
ZipInputStream zis = new ZipInputStream(cis);
// 解压操作
doDecompress(destFile, zis);
zis.close();
}

private static void doDecompress(File destFile, ZipInputStream zis) throws Exception {
ZipEntry zipEntry = null;
while ((zipEntry = zis.getNextEntry()) != null) {
String dir = destFile.getPath() + File.separator + zipEntry.getName();
File dirFile = new File(dir);
// 如果父文件夹不存在,则递归创建其父文件夹
fileProbe(dirFile);
if (zipEntry.isDirectory()) {
// 如果zipEntry是目录,则创建目录
dirFile.mkdirs();
} else {
// 解压压缩文件的其中具体的一个zipEntry对象
doDecompressFile(dirFile, zis);
}
zis.closeEntry();
}
}

// 一般意义上的文件复制操作
private static void doDecompressFile(File destFile, ZipInputStream zis) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int len;
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
while ((len = zis.read(buff, 0 ,DEFAULT_BUFF_SIZE)) != -1) {
bos.write(buff, 0, len);
}
bos.close();
}

/**
* 文件探测
*
* When the parent file not exist.Create it.
*
* @param dirFile
* @throws Exception
*/
public static void fileProbe(File dirFile) throws Exception {
File parentFile = dirFile.getParentFile();
if (!parentFile.exists()) {
fileProbe(parentFile);
parentFile.mkdirs();
}
}
}
 

原文出处:https://www.cnblogs.com/DreamCatt/p/11937822.html

关于是否可以从zipinputstream获取zipentry的输入流?zipinputstream.read方法的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于GZIPInputStream转换为String、GZIPInputStream逐行读取、GZIPOutputStream GZIPInputStream、IO流之ZipInputStream和ZipOutputStream的认识及使用的相关知识,请在本站寻找。

本文标签: