Check if any Zip file is present on a given path
Solution 1
You can try this code which checks the extension of the file inside the directory and prints the filename if Zip file is present.
public static void main(String[] args)
{
// Directory path here
String path = ".";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.endsWith(".rar") || files.endsWith(".zip"))
{
System.out.println(files);
}
}
}
}
Instead of that If you want to use FilenameFilter
as told by Andrew Thompson.
You can implement FilenameFilter in your class. More help is given on this link. By this you dont need to check the extension of file . It will give you only those files which extension is being passed as a parameter.
To extract the zip file if found you can take help of the ZipInputStream
package .
You can have a look here to extract the folder.
Solution 2
How to check if Any Zip file is present on a given path?
See File.exists()
Solution 3
This unzips all files in a directory, but it's easy to modify to only unzip a specific file.
package com.wedgeless.stackoverflow;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Unzips all zip files in a directory
* @author mk
*/
public final class Unzipper
{
public static final String DOT_ZIP = ".ZIP";
public static final FilenameFilter DOT_ZIP_FILTER = new FilenameFilter()
{
@Override
public boolean accept(File dir, String name)
{
return name.toUpperCase().endsWith(DOT_ZIP);
}
};
public static void main(String[] args)
{
File dir = new File("/path/to/dir");
Unzipper unzipper = new Unzipper();
unzipper.unzipDir(dir);
}
public void unzipDir(File dir)
{
for (File file : dir.listFiles(DOT_ZIP_FILTER))
{
unzip(file);
}
}
protected void unzip(File file)
{
File dir = getZipDir(file);
try
{
dir.mkdirs();
ZipFile zip = new ZipFile(file);
Enumeration<? extends ZipEntry> zipEntries = zip.entries();
while (zipEntries.hasMoreElements())
{
ZipEntry zipEntry = zipEntries.nextElement();
File outputFile = new File(dir, zipEntry.getName());
outputFile.getParentFile().mkdirs();
if (!zipEntry.isDirectory())
{
write(zip, zipEntry, outputFile);
}
}
}
catch (IOException e)
{
dir.delete();
}
}
protected void write(ZipFile zip, ZipEntry zipEntry, File outputFile)
throws IOException
{
final BufferedInputStream input = new BufferedInputStream(zip.getInputStream(zipEntry));
final int buffsize = 1024;
final byte buffer[] = new byte[buffsize];
final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile), buffsize);
try
{
while (input.available() > 0)
{
input.read(buffer, 0, buffsize);
output.write(buffer, 0, buffsize);
}
}
finally
{
output.flush();
output.close();
input.close();
}
}
protected File getZipDir(File zip)
{
File dir = zip.getParentFile();
int index = zip.getName().toUpperCase().lastIndexOf(DOT_ZIP);
String zipPath = zip.getName().substring(0, index);
File zipDir = new File(dir, zipPath);
return zipDir;
}
}
It's also not puzzled when on those rare occasions you get zip files with a extension in a non-standard case e.g. file.ZIP
edit: I guess I should have read the question more carefully. You only asked how to identify if a zip file existed on a path, not how to extract it. Just use the FilenameFilter approach... if you get any hits return true, otherwise false.
Comments
-
Code Hungry almost 2 years
I want to check whether any Zip file is present on a specified path. If present then I want to extract that file on the same path.
How to check if any Zip file is present on a given path?
-
Andrew Thompson almost 12 years
folder.listFiles()
This whereFilenameFilter
come in handy. -
Code Hungry almost 12 years@Andrew Thompson: vikiii is correct i want to check whether the zip file is present or not.
-
Anthony Grist almost 12 years@vikiiii I think Andrew's point was that, rather than returning a complete list of files in the directory and then iterating through them, you should pass a
FilenameFilter
that checks for the.zip
extension, thereby only returning the files you care about.