我正在尝试编写一个函数,它将接受InputStream带有压缩文件数据的函数,并将返回另一个InputStream带有解压缩数据的函数.
压缩文件只包含一个文件,因此不需要创建目录等...
我试着ZipInputStream和其他人一起看,但我对Java中的这么多不同类型的流感到困惑.
hel*_*ios 46
概念
GZipinputstream用于流(或文件)ziped为gzip(".gz"扩展名).它没有任何标题信息.
// ITS PSEUDOCODE!!
private InputStream extractOnlyFile(String path) {
ZipFile zf = new ZipFile(path);
Enumeration e = zf.entries();
ZipEntry entry = (ZipEntry) e.nextElement(); // your only file
return zf.getInputStream(entry);
}
Run Code Online (Sandbox Code Playgroud)
如果您有一个真正的zip文件,您必须使用ZipFile打开该文件,询问文件列表(在您的示例中为一个)并请求解压缩的输入流.
import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] args) throws Exception
{
FileInputStream fis = new FileInputStream("c:/inas400.zip");
// this is where you start, with an InputStream containing the bytes from the zip file
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry;
// while there are entries I process them
while ((entry = zis.getNextEntry()) != null)
{
System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
// consume all the data from this entry
while (zis.available() > 0)
zis.read();
// I could close the entry, but getNextEntry does it automatically
// zis.closeEntry()
}
}
}
Run Code Online (Sandbox Code Playgroud)
你的方法,如果你有文件,将是这样的:
// ITS PSEUDOCODE!!
private InputStream extractOnlyFile(String path) {
ZipFile zf = new ZipFile(path);
Enumeration e = zf.entries();
ZipEntry entry = (ZipEntry) e.nextElement(); // your only file
return zf.getInputStream(entry);
}
Run Code Online (Sandbox Code Playgroud)
使用.zip文件的内容读取InputStream
好吧,如果您有一个InputStream,您可以使用(如@cletus所说)ZipInputStream.它读取包含标题数据的流.
import java.io.FileInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Main {
public static void main(String[] args) throws Exception
{
FileInputStream fis = new FileInputStream("c:/inas400.zip");
// this is where you start, with an InputStream containing the bytes from the zip file
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry entry;
// while there are entries I process them
while ((entry = zis.getNextEntry()) != null)
{
System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
// consume all the data from this entry
while (zis.available() > 0)
zis.read();
// I could close the entry, but getNextEntry does it automatically
// zis.closeEntry()
}
}
}
Run Code Online (Sandbox Code Playgroud)
重要提示:如果您的PC中有该文件,您可以使用ZipFile类随机访问它
这是通过InputStream读取zip文件的示例:
// ITS PSEUDOCODE!!
private InputStream extractOnlyFile(String path) {
ZipFile zf = new ZipFile(path);
Enumeration e = zf.entries();
ZipEntry entry = (ZipEntry) e.nextElement(); // your only file
return zf.getInputStream(entry);
}
Run Code Online (Sandbox Code Playgroud)
如果您可以更改我建议您使用的输入数据GZIPInputStream.
GZipInputStream不同,ZipInputStream因为你里面只有一个数据.所以整个输入流代表整个文件.在ZipInputStream整个流中还包含其中的文件结构,可以是很多.
它是在 Scala 语法上的:
def unzipByteArray(input: Array[Byte]): String = {
val zipInputStream = new ZipInputStream(new ByteArrayInputStream(input))
val entry = zipInputStream.getNextEntry
IOUtils.toString(zipInputStream, StandardCharsets.UTF_8)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
58708 次 |
| 最近记录: |