简体   繁体   中英

some queries regarding usage of java.util.z iip.*

I have with me code for unzipping the contents of a zip file (if a directory has to be created then that is also created).

However I want to check for the following - (1) When unzipping files from the zip, check if a file is already existing, and depending on value specified by user at run time, either overwrite that file or let original file remain.

Specifically what should be the line of code below the line specified, which checks if a file of specific name is already present in that location, or not?

The line of code before which this check (for existence of file) should be made is given below separately...

copyInputStream(zipFile.getInputStream(entry),
           new BufferedOutputStream(new FileOutputStream(entry.getName())));

(2) How to check if a directory of specified name already exists or not. This check is required just before the following code--

(new File(entry.getName())).mkdir();

The entire code for program is given below-

import java.io.*;
import java.util.*;
import java.util.zip.*;


public class Unzip {

  public static final void copyInputStream(InputStream in, OutputStream out)
  throws IOException
  {
    byte[] buffer = new byte[1024];
    int len;

    while((len = in.read(buffer)) >= 0)
      out.write(buffer, 0, len);

    in.close();
    out.close();
  }

  public static final void main(String[] args) {
    Enumeration entries;
    ZipFile zipFile;

    if(args.length != 1) {
      System.err.println("Usage: Unzip zipfile");
      return;
    }

    try {
      zipFile = new ZipFile(args[0]);

      entries = zipFile.entries();

      while(entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry)entries.nextElement();

        if(entry.isDirectory()) {
          // Assume directories are stored parents first then children.
          System.err.println("Extracting directory: " + entry.getName());
          // This is not robust, just for demonstration purposes.
          (new File(entry.getName())).mkdir();
          continue;
         }

        System.err.println("Extracting file: " + entry.getName());
        copyInputStream(zipFile.getInputStream(entry),
           new BufferedOutputStream(new FileOutputStream(entry.getName())));
      }

      zipFile.close();
    } catch (IOException ioe) {
      System.err.println("Unhandled exception:");
      ioe.printStackTrace();
      return;
    }
  }

} 

您可能要在java.io.File API文档中搜索单词“ exists”。

You will need to check if folders or directory exists using java.io.File , like this:

// first obtain the base path where you are running your code
URL url = getClass().getClassLoader().getResource(".");

// then check for the existence of the file you need
File f = new File(url.getPath() + entry.getName());

// check for the flag to overwrite the file or it doesn't exist
if(!file.exists() || overwrite) {

    copyInputStream(zipFile.getInputStream(entry),
               new BufferedOutputStream(new FileOutputStream(entry.getName())));

}

The checking of the existence of the folder could be do it using the same approach.

File class have exists , isFile and isDirectory methods to serve your purpose.

Just use something like:

boolean ovrParam = // read flag from args array

// and inside the loop
File file = new File(entry.getName());
if(ovrParam || !file.exists()) {
   copyInputStream(zipFile.getInputStream(entry),
       new BufferedOutputStream(new FileOutputStream(entry.getName())));
}

Of course combining directories and files can me more tricky (for example, you can have an folder in your file system extracting location, and the zip contains a file with the precisely same name of your folder to be unzipped, or maybe the inverse). But anyway this should give you a guideline.

Cheers,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM