Java Reference
In-Depth Information
System.out.println("Output has been written to " +
currentDirectory + File.separator + zipFileName);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void addEntryContent(ZipOutputStream zos,
String entryFileName)
throws IOException, FileNotFoundException {
// Create an input stream to read data from the entry file
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(entryFileName));
byte[] buffer = new byte[1024];
int count = -1;
while ((count = bis.read(buffer)) != -1) {
zos.write(buffer, 0, count);
}
bis.close();
}
}
Output has been written to C:\book\javabook\ziptest.zip
Reading contents from a ZIP file is just the opposite of writing contents to it. Here are the steps to read the
contents (or extract entries) of a ZIP file.
1.
Create a ZipInputStream object.
2.
Get a ZipEntry from the input stream calling the getNextEntry() method of the
ZipInputStream object.
3.
Read the data for the ZipEntry from the ZipInputStream object.
4.
Repeat the previous two steps to read another zip entry from the archive.
5. Close the ZipInputStream .
You can create a ZipInputStream object using the ZIP file name as follows:
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(
new FileInputStream(zipFileName)));
The following snippet of code gets the next entry from the input stream:
ZipEntry entry = zis.getNextEntry();
 
Search WWH ::




Custom Search