I have checked java api document, it says getNextEntry() Reads the next ZIP file entry and positions the stream at the beginning of the entry data.
what does it mean "reads the NEXT zip file"? why the "NEXT" ?
I have this piece of code, what's the point of this line ze = zin.getNextEntry()?
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.e("Decompress", "unzip", e);
}
}
It reads the next entry within the zip file.
A zip file logically contains main other files - so foo.zip can contain files a.txt and b.txt. The getNextEntry() moves you on to the next file within the archive.
(I've never been particularly keen on the way that ZipInputStream is modeled using inheritance from InputStream, but that's a different matter.)
Related
I am trying to delete a zip file after unziping. but I am not able to delete it:
if (file.getName().contains(".zip")) {
System.out.println(file.getAbsolutePath()); // I am getting the correct path
file.delete();
System.out.println(file.getName()); // I am getting the correct name Script-1.zip
}
This is the full code
public class Zip4 {
public static void main(String[] args) {
File[] files = new File(args[0]).listFiles();
for(File file : files)
// System.out.println(file.getName());
//if(file.getName().contains("1400") && file.getName().contains(".zip"))
extractFolder(args[0] + file.getName(), args[1]);
DeleteFiles();
// for(File file : files)
// System.out.println("File:C:/1/"+ file.getName());
// extractFolder(args[0]+file.getName(),args[1]);
}
private static void DeleteFiles()
{
File f = null;
File[] paths;
f = new File("D:/Copyof");
paths = f.listFiles();
for(File path:paths)
{
// prints file and directory paths
if(path.getName().contains("J14_0_0RC") || path.getName().contains(".zip") || path.getName().contains(".log"))
{
//System.out.println(path);
path.delete();
}
}
}
private static void extractFolder(String zipFile,String extractFolder)
{
try
{
int BUFFER = 2048;
File file = new File(zipFile);
ZipFile zip = new ZipFile(file);
String newPath = extractFolder;
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
// Process each entry
while (zipFileEntries.hasMoreElements())
{
// grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
//destFile = new File(newPath, destFile.getName());
File destinationParent = destFile.getParentFile();
// create the parent directory structure if needed
destinationParent.mkdirs();
if (!entry.isDirectory())
{
BufferedInputStream is = new BufferedInputStream(zip
.getInputStream(entry));
int currentByte;
// establish buffer for writing file
byte data[] = new byte[BUFFER];
// write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
// read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
fos.flush();
fos.close();
is.close();
}
}
if(file.getName().contains(".zip"))
{
System.out.println(file.getAbsolutePath());
file.delete();
System.out.println(file.getName());
}
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
}
}
}
ZipFile is a closeable resource. So either close() it once you're done in a finally block or create it with try-with-resources (since java7):
try(ZipFile zip = new ZipFile(file)){
//unzip here
}
file.delete();
Apart from this, you should revisit this block
dest.flush();
dest.close();
fos.flush();
fos.close();
is.close();
which is quite prone to resource leaks. If one of the upper calls fails, all subsequent calls are not invoked, resulting in unclosed resources and resource leakage.
So best would be to use try-with-resources here, too.
try(BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
//write the data
} //all streams are closed implicitly here
Or use an existing tool for that, for example Apache Commons IO IOUtil.closeQuietly(resource) or embedd every single call into
if(resource != null) {
try{
resource.close();
} catch(IOException e){
//omit
}
}
You could also omit the call to flush() which is done implicitly when closing the resource.
I kind of stuck in this problem. I want to print only the top level directory from a zip file. For example I have a zip file with following structure:
Sample.zip
- sound
- game
-start.wav
-end.wav
- Intro
- custom
- Scene
- fight
- Angle
..............
Above figure shows: the Sample.zip has 2 folders (sound and custom), and inside sound there are 2 folders game and Intro and so on...
Now I know how to open and grab the directory from zip file: For example (working code)
try {
appFile = ("../../Sample.zip"); // just the path to zip file
ZipFile zipFile = new ZipFile(appFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if(entry.isDirectory()){
String dir = entry.getName();
File file = new File(dir);
System.out.println(file.getParent());
}
}
} catch (IOException e) {
System.out.println("Error opening Zip" +e);
}
Now I also know I can use .getParent()(as you see above) to get the top level dir, but the above implementation has not worked. It'll list out all the directory , like
null
sound
game
null
custom
scene
Angle
My question is how can I actually print only the top level folders, In above scenario , sound and custom ?
For any sort of hint, I'll be thankful.
Actually I did following as suggested by #JB Nizet and get a work around(it actually work ):
try {
appFile = ("../../Sample.zip"); // just the path to zip file
ZipFile zipFile = new ZipFile(appFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if(entry.isDirectory()){
File file = new File(entry.getName());
if(file.getParent() == null){
System.out.println(file.getName());
}
}
}
} catch (IOException e) {
System.out.println("Error opening Zip" +e);
}
The above solution has worked because the top level dir has no parent and therefore returned null as output. So I just loop around the directories to see if they have parents, if they dont have any parent then they are top level directory.
You can use something like that:
try{
String appFile = "../file.zip"; // just the path to zip file
ZipFile zipFile = new ZipFile(appFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if(entry.isDirectory() && !entry.getName().matches("\\S+/\\S+")){ //it's a top level folder
System.out.println(entry.getName());
}
}
} catch (IOException e) {
System.out.println("Error opening Zip" +e);
}
Maybe this code will help you with using InputStream
String topFolder="";
String topFolder2="";
Boolean hasTopFolder=true;
try{
File dir = new File(path+"/html5"+catalogue.getIdProduit());
if (!dir.exists()) {
dir.mkdirs();
}
String outputFolder= "path/to/outputFolder";
InputStream input = file.getInputstream();
//get the zip file content
ZipInputStream zis = new ZipInputStream(input);
//get the zipped file list entry
ZipEntry ze = zis.getNextEntry();
while(ze!=null){
if (ze.isDirectory()) {
System.out.println("is directory : "+ ze.getName());
if ("".equals(topFolder)){
topFolder = ze.getName().split("/")[0];
System.out.println("is directory topFolder : "+ ze.getName());
}
if (("".equals(topFolder2)) && (!topFolder.equals(ze.getName().split("/")[0]))){
hasTopFolder=false;
topFolder2=ze.getName().split("/")[0];
System.out.println("is directory topFolder2 : "+ ze.getName());
}
ze = zis.getNextEntry();
continue;
}
String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);
System.out.println("file unzip : "+ newFile.getAbsoluteFile());
//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("Done");
}
catch(IOException e){
e.printStackTrace();
}
if (hasTopFolder){
topFolder="/"+topFolder;
}
else
topFolder="";
What about following method:
/**
* Get the root folders within a zip file
*
* #param zipFile the zip file to be used. E.g. '/home/foo/bar.zip'
* #return a list containing all root folders
* #throws Exception if case the zip file cannot be found or read.
*/
public static List<String> getGetRootDirectoriesWithinZip(String zipFile) throws Exception {
Set<String> set = new LinkedHashSet();
//get the zip file content stream
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile));
//get the zipped file set entry
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
Path path = Paths.get(fileName);
int nameCount = path.getNameCount();
for (int i = 0; i < nameCount; i++) {
if (path != null && path.getParent() != null) {
path = path.getParent();
}
}
set.add(path.toString());
zipEntry = zipInputStream.getNextEntry();
}
List<String> retList = new ArrayList<>();
retList.addAll(set);
return retList;
}
This is the method that worked for me.
I should note that I am using StringUtils (Apache Lang3) to count how many times
"\" appears in the ZipEntry path, although if you don't want to use StringUtils you could make
your own method for counting.
public static ArrayList<ZipEntry> getZipContent(File file, int index) {
try {
ArrayList<String> innerFoldersPaths = new ArrayList<String>();
ArrayList<ZipEntry> retEntries = new ArrayList<ZipEntry>();
ZipFile zipFile = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
// If you also want to get files remove the "if (entry.isDirectory())" statement.
if (entry.isDirectory()) {
String backSlashName = entry.getName().replace("/", "\\"); // Important to do this.
if (StringUtils.countMatches(backSlashName, "\\") > index - 1) { // Using Apache StringUtils
String folder[] = backSlashName.split(Pattern.quote("\\"));
String finalFolder = "";
// Getting the folders path inside the .zip file .
for (int i = 0; i < index; i++) {
folder[i] = folder[i] + "\\";
finalFolder = finalFolder + folder[i];
}
finalFolder = finalFolder.replace("\\", "/"); // Important to do this.
if (innerFoldersPaths.contains(finalFolder)) {
} else {
innerFoldersPaths.add(finalFolder);
}
}
}
}
for (String backSlashName : innerFoldersPaths) {
retEntries.add(zipFile.getEntry(backSlashName));
}
zipFile.close();
return retEntries;
} catch (Exception exception) {
// handle the exception in the way you want.
exception.printStackTrace();
}
return null;
}
The usage of this method:
File file = new File("Your zip file here");
for (ZipEntry zipEntry : getZipContent(file, 1)) { // This would return all the folders in the first file
// Do what ever your wantt with the ZipEntry
System.out.println(zipEntry.getName());
}
If you want to get all the folders past the first one,
you could do it by changing the index to how deep the folders that you want to get are.
use this fun in Kotlin
fun getRootFolderName(fileAddress: String): String {
if (File(fileAddress).parent == null || ("" + File(fileAddress).parent).length < 1) return File(fileAddress).name
return getRootFolderName("" + File(fileAddress).parent)
}
When I am trying to extract the zip file into a folder as per the below code, for one of the entry (A text File) getting an error as "Invalid entry size (expected 46284 but got 46285 bytes)" and my extraction is stopping abruptly. My zip file contains around 12 text files and 20 TIF files. It is encountering the problem for the text file and is not able to proceed further as it is coming into the Catch block.
I face this problem only in Production Server which is running on Unix and there is no problem with the other servers(Dev, Test, UAT).
We are getting the zip into the servers path through an external team who does the file transfer and then my code starts working to extract the zip file.
...
int BUFFER = 2048;
java.io.BufferedOutputStream dest = null;
String ZipExtractDir = "/y34/ToBeProcessed/";
java.io.File MyDirectory = new java.io.File(ZipExtractDir);
MyDirectory.mkdir();
ZipFilePath = "/y34/work_ZipResults/Test.zip";
// Creating fileinputstream for zip file
java.io.FileInputStream fis = new java.io.FileInputStream(ZipFilePath);
// Creating zipinputstream for using fileinputstream
java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(new java.io.BufferedInputStream(fis));
java.util.zip.ZipEntry entry;
while ((entry = zis.getNextEntry()) != null)
{
int count;
byte data[] = new byte[BUFFER];
java.io.File f = new java.io.File(ZipExtractDir + "/" + entry.getName());
// write the files to the directory created above
java.io.FileOutputStream fos = new java.io.FileOutputStream(ZipExtractDir + "/" + entry.getName());
dest = new java.io.BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1)
{
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
zis.closeEntry();
}
catch (Exception Ex)
{
System.Out.Println("Exception in \"ExtractZIPFiles\"---- " + Ex.getMessage());
}
I can't understand the problem you're meeting, but here is the method I use to unzip an archive:
public static void unzip(File zip, File extractTo) throws IOException {
ZipFile archive = new ZipFile(zip);
Enumeration<? extends ZipEntry> e = archive.entries();
while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement();
File file = new File(extractTo, entry.getName());
if (entry.isDirectory()) {
file.mkdirs();
} else {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
InputStream in = archive.getInputStream(entry);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
IOUtils.copy(in, out);
in.close();
out.close();
}
}
}
Calling:
File zip = new File("/path/to/my/file.zip");
File extractTo = new File("/path/to/my/destination/folder");
unzip(zip, extractTo);
I never met any issue with the code above, so I hope that could help you.
Off the top of my head, I could think of these reasons:
There could be problem with the encoding of the text file.
The file needs to be read/transferred in "binary" mode.
There could be an issue with the line ending \n or \r\n
The file could simply be corrupt. Try opening the file with a zip utility.
I was looking through java documentation and there doesn't seem to be a way to specifically unzip saz files, which are session archives created by the network proxy Fiddler. Anyone have any idea of how to do it?
According this it is regular ZIP with specific extension for file name. Use java.util.zip.ZipFile.
About not having specific method for this extension - I think it kind of makes sense to not to have specific method for every possible extension.
This should do the trick
saz files are regular zip files
public static boolean unzipFiles(String srcDirectory, String srcFile, String destDirectory)
{
try
{
//first make sure that all the arguments are valid and not null
if(srcDirectory == null)
{
System.out.println(1);
return false;
}
if(srcFile == null)
{
System.out.println(2);
return false;
}
if(destDirectory == null)
{
System.out.println(3);
return false;
}
if(srcDirectory.equals(""))
{
System.out.println(4);
return false;
}
if(srcFile.equals(""))
{
System.out.println(5);
return false;
}
if(destDirectory.equals(""))
{
System.out.println(6);
return false;
}
//now make sure that these directories exist
File sourceDirectory = new File(srcDirectory);
File sourceFile = new File(srcDirectory + File.separator + srcFile);
File destinationDirectory = new File(destDirectory);
if(!sourceDirectory.exists())
{
System.out.println(7);
return false;
}
if(!sourceFile.exists())
{
System.out.println(sourceFile);
return false;
}
if(!destinationDirectory.exists())
{
System.out.println(9);
return false;
}
//now start with unzip process
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(sourceFile);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry = null;
while((entry = zis.getNextEntry()) != null)
{
String outputFilename = destDirectory + File.separator + entry.getName();
System.out.println("Extracting file: " + entry.getName());
createDirIfNeeded(destDirectory, entry);
int count;
byte data[] = new byte[BUFFER_SIZE];
//write the file to the disk
FileOutputStream fos = new FileOutputStream(outputFilename);
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
while((count = zis.read(data, 0, BUFFER_SIZE)) != -1)
{
dest.write(data, 0, count);
}
//close the output streams
dest.flush();
dest.close();
}
//we are done with all the files
//close the zip file
zis.close();
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
This page, found by googling "saz files", says:
SAZ files are simply specially formatted .ZIP files. If you rename a
.SAZ file to .ZIP, you can open it for viewing using standard ZIP
viewing tools
So they're just zip files with a different extension. Unzip them exactly like you would unzip a zip file.
java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(new BufferedInputStream(is));
java.util.zip.ZipEntry entry;
new File(outdir+ File.separator+"changelog").delete();
new File(outdir+ File.separator+"media").delete();
try {
while ((entry = zis.getNextEntry()) != null) {
File of = new File(outdir + File.separator + entry.getName());
if (entry.isDirectory()) {
of.mkdirs();
continue;
} else {
File xx = new File(of.getParent());
if (!xx.exists()) {
Stack<String> todo = new Stack<String>();
do {
todo.push(xx.getAbsolutePath());
xx = new File(xx.getParent());
} while (!xx.exists());
while (todo.size() > 0) {
xx = new File(todo.pop());
if (!xx.exists()) {
xx.mkdirs();
}
}
}
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(of), buffer.length);
cpio(new BufferedInputStream(zis), bos, "unzip:" + entry.getName());
bos.flush();
bos.close();
}
} catch (IllegalArgumentException e) {
// problem with chars in entry name likely
}catch(Exception e){
System.out.println(e+"Srikanth");
}
zis.close();
}
entry.isDirectory() always returing false so it is creating files instead of directories. What is the problem?
ZipEntry from ZipInputStream representing empty directory at end of file with \ , directory with elements with /
So that entry.isDirectory() not working with empty directory.
Where as ZipEntry from ZipFile working fine. I think there is difference between ZipInputStream and ZipEntry behavior.
isDirectory is not working at all with files zipped with Windows standard option "sent to/ zip files"
The format of the zip is different than format generated with tools like 7zip or Winzip. (Nice to have a standard archive compression :D)