Code Hungry
Code Hungry

Reputation: 4000

Check if any Zip file is present on a given path

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?

Upvotes: 2

Views: 11176

Answers (5)

KidTempo
KidTempo

Reputation: 930

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.

Upvotes: 1

vikiiii
vikiiii

Reputation: 9456

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.

Upvotes: 2

user207421
user207421

Reputation: 310884

The best way to test the availability of any resource that you want to use is just to try to use it. Any other technique is vulnerable to timing-window problems, critique that you are trying to predict the future, double coding, etc. etc. etc. Just catch the FileNotFoundException that happens when you try to open it. You have to catch it anyway, why write the same code twice?

Upvotes: 0

Mark
Mark

Reputation: 1100

Take a File variable and go through all the files inside the director and ckeck for a .zip or .tar.gz or .rar How to iterate over the files of a certain directory, in Java?

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168825

How to check if Any Zip file is present on a given path?

See File.exists()

Upvotes: 1

Related Questions