我有一个.gz格式的文件.用于读取此文件的java类是GZIPInputStream.但是,此类不会扩展Java的BufferedReader类.结果,我无法逐行读取文件.我需要这样的东西
reader = new MyGZInputStream( some constructor of GZInputStream)
reader.readLine()...
Run Code Online (Sandbox Code Playgroud)
我创建了我的类,它扩展了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, …Run Code Online (Sandbox Code Playgroud) 首先,如果我的术语有点业余,我很抱歉,试着忍受我;)
我试图将HTTP响应的gzipped主体转换为明文.我已经采用了这个响应的字节数组并将其转换为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, 0, sChunk)) != -1) {
out.write(buffer, 0, length);
}
Run Code Online (Sandbox Code Playgroud) 我有一个GZIPInputStream我从另一个构建的ByteArrayInputStream.我想知道gzip数据的原始(未压缩)长度.虽然我可以阅读到最后GZIPInputStream,然后计算数量,它将花费很多时间和浪费CPU.我想在阅读之前知道尺寸.
有没有像一个类似的方法ZipEntry.getSize()为GZIPInputStream:
public long getSize ()
从以下版本开始: API Level 1
获取此ZipEntry的未压缩大小.
我注意到我的一些gzip解码代码似乎无法检测到损坏的数据.我认为我已将问题追溯到Java GZipInputStream类.特别是,当您使用单个"读取"调用读取整个流时,损坏的数据似乎不会触发IOException.如果您在同一个损坏的数据上读取2次或多次调用的流,则会触发异常.
在考虑提交错误报告之前,我想看看社区在这里想到了什么.
编辑:我修改了我的例子,因为最后一个没有清楚地说明我认为是什么问题.在这个新的例子中,gzip压缩了一个10字节的缓冲区,修改了gzip压缩缓冲区的一个字节,然后对其进行了解压缩.对'GZipInputStream.read'的调用返回10作为读取的字节数,这是您对10字节缓冲区的期望.然而,解压缩的缓冲区与原始缓冲区不同(由于损坏).没有异常被抛出.我注意到在读取后调用'available'会返回'1'而不是'0',如果已达到EOF,则会返回.
这是来源:
@Test public void gzip() {
try {
int length = 10;
byte[] bytes = new byte[]{12, 19, 111, 14, -76, 34, 60, -43, -91, 101};
System.out.println(Arrays.toString(bytes));
//Gzip the byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos);
gos.write(bytes);
gos.finish();
byte[] zipped = baos.toByteArray();
//Alter one byte of the gzipped array.
//This should be detected by gzip crc-32 checksum
zipped[15] = (byte)(0);
//Unzip the modified array
ByteArrayInputStream bais = new ByteArrayInputStream(zipped);
GZIPInputStream gis …Run Code Online (Sandbox Code Playgroud) 我调用一个返回gzip压缩文件的服务.我javax.activation.DataHandler.getInputStream();从响应中将数据作为InputStream(礼貌).
我想要做的是,在不向磁盘写入任何内容的情况下,在归档文件中的文件中获取解压缩数据的InputStream.在这种情况下,压缩文件是一个xml文档,我试图解组使用javax.xml.bind.Unmarshaller它来获取InputStream.
我正在尝试将InputStream写入OutputStream(解压缩数据),然后我需要将其写回InputStream.它还没有工作,所以我想我会看看是否有更好的(我希望如此)方法.
我可以将初始InputStream写入磁盘并获取一个gz文件,然后读取该文件,从中获取压缩文件并从那里开始,但我宁愿将其全部保存在内存中也是可能的.
更新1:这是我的当前(不工作 - 得到"不是GZIP格式"的例外):
ByteArrayInputStream xmlInput = null;
try {
InputStream in = dh.getInputStream(); //dh is a javax.activation.DataHandler
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int bytes_read = 0;
byte[] dataBuf = new byte[4096];
while ((bytes_read = bis.read(dataBuf)) != -1) {
bo.write(dataBuf, 0, bytes_read);
}
ByteArrayInputStream bin = new ByteArrayInputStream(bo.toByteArray());
GZIPInputStream gzipInput = new GZIPInputStream(bin);
ByteArrayOutputStream out = new ByteArrayOutputStream();
dataBuf = new byte[4096];;
bytes_read = 0;
while ((bytes_read = …Run Code Online (Sandbox Code Playgroud) 我在我的程序中使用GZIPInputStream,我知道如果我可以让Java并行运行我的程序,性能会有所帮助.
通常,标准VM是否有一个命令行选项可以在许多内核上运行?它只运行一个.
谢谢!
编辑
我正在Windows XP上运行普通的'Java SE 6更新17'.
将GZIPInputStream放在一个单独的线程上显然会有帮助吗?没有!不要将GZIPInputStream放在单独的线程上!不要多线程I/O!
编辑2
我想I/O是瓶颈,因为我正在读写同一个磁盘......
但总的来说,有没有办法让GZIPInputStream更快?或者是并行运行的GZIPInputStream的替代品?
编辑 我使用的3个代码片段:
GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(INPUT_FILENAME));
DataInputStream in = new DataInputStream(new BufferedInputStream(gzip));
Run Code Online (Sandbox Code Playgroud) 我是最有经验的JAVA用户,但是,我对我的问题非常绝望.每次执行以下代码时,都会收到以下错误:
java.util.zip.ZipException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(Unknown Source)
at java.util.zip.GZIPInputStream.(init)(Unknown Source)
at java.util.zip.GZIPInputStream.(init)(Unknown Source)
at DidYouMean.executeGet(DidYouMean.java:56)
at DidYouMean.didYouMean(DidYouMean.java:11)
at DidYouMean.main(DidYouMean.java:39)
Exception in thread "main" java.lang.IllegalArgumentException: String input must not be null....
Run Code Online (Sandbox Code Playgroud)
我的一个朋友(使用Mac而不是我使用Windows 7 64)能够执行该程序.所以它似乎不是代码本身的问题(这是由Github上的某人开发的).我真的很感激任何帮助!我对解决方案的搜索并不是很成功,即使错误并不罕见.
import java.io.*;
import java.net.*;
import org.jsoup.*;
import java.util.zip.*;
import org.jsoup.nodes.*;
import org.jsoup.examples.HtmlToPlainText;
public class DidYouMean {
public static String didYouMean(String s){
String word="";
String url="http://www.google.co.in/search?hl=en&q="+URLEncoder.encode(s);
String html=executeGet(url,"www.google.co.in",'i');
Document content=Jsoup.parse(html);
Element submitted=null;
try{
submitted=content.getElementById("topstuff").clone();
HtmlToPlainText h=new HtmlToPlainText();
word=h.getPlainText(submitted);
int q,p=word.indexOf("Did you mean:");
if(p>=0){
word=word.substring(p+"Did you mean:".length()); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下Java代码来压缩和解压缩String.但是,从新的ByteArrayInputStream对象创建新GZipInputStream对象的行会抛出"java.util.zip.ZipException:Not in GZIP format"异常.有谁知道如何解决这个问题?
String orig = ".............";
// compress it
ByteArrayOutputStream baostream = new ByteArrayOutputStream();
OutputStream outStream = new GZIPOutputStream(baostream);
outStream.write(orig.getBytes());
outStream.close();
String compressedStr = baostream.toString();
// uncompress it
InputStream inStream = new GZIPInputStream(new ByteArrayInputStream(compressedStr.getBytes()));
ByteArrayOutputStream baoStream2 = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int len;
while((len = inStream.read(buffer))>0)
baoStream2.write(buffer, 0, len);
String uncompressedStr = baoStream2.toString();
Run Code Online (Sandbox Code Playgroud) 我有一个类,它有一个接收对象作为参数的方法.通过RMI调用此方法.
public RMIClass extends Serializable {
public RMIMethod(MyFile file){
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
MyFile有一个名为"body"的属性,它是一个字节数组.
public final class MyFile implements Serializable {
private byte[] body = new byte[0];
//....
public byte[] getBody() {
return body;
}
//....
}
Run Code Online (Sandbox Code Playgroud)
此属性包含由另一个应用程序解析的文件的gzip压缩数据.
在执行进一步的操作之前,我需要解压缩此字节数组.
我看到的解压缩gzip压缩数据的所有例子都假设我想将它写入磁盘并创建一个物理文件,我不这样做.
我该怎么做呢?
提前致谢.
是否有适用于J2ME的GZIPInputStream实现.我更喜欢Apache风格许可证
gzipinputstream ×10
java ×9
gzip ×8
compression ×2
io ×2
file-io ×1
filereader ×1
http ×1
java-me ×1
jsoup ×1
multicore ×1