How to read file from ZIP using InputStream?
Well, I've done this:
zipStream = new ZipInputStream(channelSftp.get("Port_Increment_201405261400_2251.zip"));
zipStream.getNextEntry();
sc = new Scanner(zipStream);
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
It helps me to read ZIP's content without writing to another file.
Getting specific file from ZipInputStream
If the myInputStream
you're working with comes from a real file on disk then you can simply use java.util.zip.ZipFile
instead, which is backed by a RandomAccessFile
and provides direct access to the zip entries by name. But if all you have is an InputStream
(e.g. if you're processing the stream directly on receipt from a network socket or similar) then you'll have to do your own buffering.
You could copy the stream to a temporary file, then open that file using ZipFile
, or if you know the maximum size of the data in advance (e.g. for an HTTP request that declares its Content-Length
up front) you could use a BufferedInputStream
to buffer it in memory until you've found the required entry.
BufferedInputStream bufIn = new BufferedInputStream(myInputStream);
bufIn.mark(contentLength);
ZipInputStream zipIn = new ZipInputStream(bufIn);
boolean foundSpecial = false;
while ((entry = zin.getNextEntry()) != null) {
if("special.txt".equals(entry.getName())) {
// do whatever you need with the special entry
foundSpecial = true;
break;
}
}
if(foundSpecial) {
// rewind
bufIn.reset();
zipIn = new ZipInputStream(bufIn);
// ....
}
(I haven't tested this code myself, you may find it's necessary to use something like the commons-io CloseShieldInputStream
in between the bufIn
and the first zipIn
, to allow the first zip stream to close without closing the underlying bufIn
before you've rewound it).
Unzipping a file from InputStream and returning another InputStream
Concepts
GZIPInputStream is for streams (or files) zipped as gzip (".gz" extension). It doesn't have any header information.
This class implements a stream filter for reading compressed data in the GZIP file format
If you have a real zip file, you have to use ZipFile to open the file, ask for the list of files (one in your example) and ask for the decompressed input stream.
Your method, if you have the file, would be something like:
// 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);
}
Reading an InputStream with the content of a .zip file
Ok, if you have an InputStream you can use (as @cletus says) ZipInputStream. It reads a stream including header data.
ZipInputStream is for a stream with [header information + zippeddata]
Important: if you have the file in your PC you can use ZipFile
class to access it randomly
This is a sample of reading a zip-file through an InputStream:
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()
}
}
}
Read Content from Files which are inside Zip file
If you're wondering how to get the file content from each ZipEntry
it's actually quite simple. Here's a sample code:
public static void main(String[] args) throws IOException {
ZipFile zipFile = new ZipFile("C:/test.zip");
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while(entries.hasMoreElements()){
ZipEntry entry = entries.nextElement();
InputStream stream = zipFile.getInputStream(entry);
}
}
Once you have the InputStream you can read it however you want.
How to get an InputStream representing one file in a ZipInputStream
You're virtually there. Try
private InputStream getReqifInputStreamFrom(byte[] reqifzFileBytes) throws IOException {
InputStream result = null;
ByteArrayInputStream inputStream = new ByteArrayInputStream(reqifzFileBytes);
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
if (fileName.endsWith(REQIF_FILE_SUFFIX)) {
result = zipInputStream;
break;
}
zipEntry = zipInputStream.getNextEntry();
}
return result;
}
Related Topics
How to Serialize Only the Id of a Child with Jackson
How to Make a Countdown Timer in Java
Running Selenium Scripts with Jmeter
Is Doing a Lot in Constructors Bad
Log4J2 - Assigning File Appender Filename at Runtime
Generating Rsa Keys in Pkcs#1 Format in Java
How to Pass C Structs Back and Forth to Java Code in Jni
Are There Any Other Java Libraries for Bonjour/Zeroconf Apart from Jmdns
What Is the Equivalent of Java Static Final Fields in Kotlin
Invokeandwait Method in Swingutilities
Convert Bufferedinputstream into Image
Display Numbers from 1 to 100 Without Loops or Conditions
Setting Jvm/Jre to Use Windows Proxy Automatically