Read Content from Files Which Are Inside Zip File

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 read content of files inside Zip file which is inside a Zip file

ZipFile zip = new ZipFile("Outer.zip");
...
zip.getInputStream(ze); // It is giving null

Contents of ze (e.g. TxtFile1.txt) are part of InnerZip.zip not Outer.zip (represented by zip), hence null.

I'd use recursion:

public static void main(String[] args) throws IOException {
String name = "Outer.zip";
FileInputStream input = new FileInputStream(new File(name));
readZip(input, name);
}

public static void readZip(final InputStream in, final String name) throws IOException {
final ZipInputStream zin = new ZipInputStream(in);
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (entry.getName().toLowerCase().endsWith(".zip")) {
readZip(zin, name + "/" + entry.getName());
} else {
readFile(zin, entry.getName());
}
}
}

private static void readFile(final InputStream in, final String name) {
String contents = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
System.out.println(String.format("Contents of %s: %s", name, contents));
}

0. while (...) we are iterating through whole all entries.

1. (if (.endsWith(".zip"))) in case we encounter another zip we call recursively itself (readZip()) and go to step 0.

2. (else) otherwise we print the contents of the file (assuming text files here).

Read Zip file content without extracting in java

Here is an example:

public static void main(String[] args) throws IOException {
ZipFile zip = new ZipFile("C:\\Users\\mofh\\Desktop\\test.zip");

for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) e.nextElement();
if (!entry.isDirectory()) {
if (FilenameUtils.getExtension(entry.getName()).equals("png")) {
byte[] image = getImage(zip.getInputStream(entry));
//do your thing
} else if (FilenameUtils.getExtension(entry.getName()).equals("txt")) {
StringBuilder out = getTxtFiles(zip.getInputStream(entry));
//do your thing
}
}
}

}

private static StringBuilder getTxtFiles(InputStream in) {
StringBuilder out = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
try {
while ((line = reader.readLine()) != null) {
out.append(line);
}
} catch (IOException e) {
// do something, probably not a text file
e.printStackTrace();
}
return out;
}

private static byte[] getImage(InputStream in) {
try {
BufferedImage image = ImageIO.read(in); //just checking if the InputStream belongs in fact to an image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
return baos.toByteArray();
} catch (IOException e) {
// do something, it is not a image
e.printStackTrace();
}
return null;
}

Keep in mind though I am checking a string to diferentiate the possible types and this is error prone. Nothing stops me from sending another type of file with an expected extension.

How to read files in a .zip file in Java?

You could put the code that reads content inside an if

ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
InputStream stream = zipFile.getInputStream(entry);
...
stream.close();
}

Read content of a txt file inside a folder which is inside a Zip file

static void Main(string[] args)
{
var dt = new DataTable("Numbers");
dt.Columns.Add(new DataColumn { DataType = typeof(int), ColumnName = "Num" });
using (var zip = ZipFile.OpenRead(@"PATH_TO_ZIP"))
{
var entry = zip.GetEntry("PATH_TO_FILE_IN_ZIP");
{
using (var stream = entry.Open())
{
using (var sr = new StreamReader(stream))
{
while(!sr.EndOfStream)
{
string line = sr.ReadLine();
var row = dt.NewRow();
row["Num"] = line;
dt.Rows.Add(row);
}
}
}
}
}
}

Reading text files in a zip archive

No, you don't need a RandomAccessFile. First get an InputStream with the data for this zip file entry:

InputStream input = zipFile.getInputStream(entry);

Then wrap it in an InputStreamReader (to decode from binary to text) and a BufferedReader (to read a line at a time):

BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF-8"));

Then read lines from it as normal. Wrap all the appropriate bits in try/finally blocks as usual, too, to close all resources.



Related Topics



Leave a reply



Submit