Example usage for java.util.zip ZipFile getName

List of usage examples for java.util.zip ZipFile getName

Introduction

In this page you can find the example usage for java.util.zip ZipFile getName.

Prototype

public String getName() 

Source Link

Document

Returns the path name of the ZIP file.

Usage

From source file:it.geosolutions.geoserver.rest.GeoServerRESTPublisher.java

/**
 * Create a new ImageMosaic with the provided configuration provided as a zip file.
 * /*from  ww  w .  j  a v  a  2s.  c o m*/
 * <p>
 * With the options configure we can decide whether or not to configure or not the coverages contained in the ImageMosaic.
 * 
 * @param workspace the GeoServer workspace
 * @param coverageStore the GeoServer coverageStore
 * @param the absolute path to the file to upload
 * @param configureOpt tells GeoServer whether to configure all coverages in this mosaic (ALL) or none of them (NONE).
 * 
 * @return <code>true</code> if the call succeeds or <code>false</code> otherwise.
 * @since geoserver-2.4.0, geoserver-mng-1.6.0
 */
public boolean createImageMosaic(String workspace, String coverageStore, String path,
        ConfigureCoveragesOption configureOpt) {
    // checks
    checkString(workspace);
    checkString(coverageStore);
    checkString(path);
    final File zipFile = new File(path);
    if (!zipFile.exists() || !zipFile.isFile() || !zipFile.canRead()) {
        throw new IllegalArgumentException(
                "The provided pathname does not point to a valide zip file: " + path);
    }
    // is it a zip?
    ZipFile zip = null;
    try {
        zip = new ZipFile(zipFile);
        zip.getName();
    } catch (Exception e) {
        LOGGER.trace(e.getLocalizedMessage(), e.getStackTrace());
        throw new IllegalArgumentException(
                "The provided pathname does not point to a valide zip file: " + path);
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (IOException e) {
                // swallow
                LOGGER.trace(e.getLocalizedMessage(), e.getStackTrace());
            }
        }
    }

    // create URL
    StringBuilder ss = HTTPUtils.append(restURL, "/rest/workspaces/", workspace, "/coveragestores/",
            coverageStore, "/", UploadMethod.EXTERNAL.toString(), ".imagemosaic");
    switch (configureOpt) {
    case ALL:
        break;
    case NONE:
        ss.append("?configure=none");
        break;
    default:
        throw new IllegalArgumentException("Unrecognized COnfigureOption: " + configureOpt);
    }
    String sUrl = ss.toString();

    // POST request
    String result = HTTPUtils.put(sUrl, zipFile, "application/zip", gsuser, gspass);
    return result != null;
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileUtilities.java

public void decompressZip(File inputZipPath, File zipPath) {
    int BUFFER = 2048;
    List<File> zipFiles = new ArrayList<File>();

    try {//from ww  w  . j a v a  2  s . c o m
        zipPath.mkdir();
    } catch (SecurityException e) {
        jlog.fatal("Security exception when creating " + zipPath.getName());

    }
    ZipFile zipFile = null;
    boolean isZip = true;

    // Open Zip file for reading (should be in temppath)
    try {
        zipFile = new ZipFile(inputZipPath, ZipFile.OPEN_READ);
    } catch (IOException e) {
        jlog.fatal("IO exception in " + inputZipPath.getName());
    }

    // Create an enumeration of the entries in the zip file
    Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
    if (isZip) {
        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            // Get a zip file entry
            ZipEntry entry = zipFileEntries.nextElement();

            String currentEntry = entry.getName();
            File destFile = null;

            // destFile should be pointing to temppath\%date%\
            try {
                destFile = new File(zipPath.getAbsolutePath(), currentEntry);
                destFile = new File(zipPath.getAbsolutePath(), destFile.getName());
            } catch (NullPointerException e) {
                jlog.fatal("File not found" + destFile.getName());
            }

            // If the entry is a .zip add it to the list so that it can be extracted
            if (currentEntry.endsWith(".zip")) {
                zipFiles.add(destFile);
            }

            try {
                // Extract file if not a directory
                if (!entry.isDirectory()) {
                    // Stream the zip entry
                    BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));

                    int currentByte;
                    // establish buffer for writing file
                    byte data[] = new byte[BUFFER];
                    FileOutputStream fos = null;

                    // Write the current file to disk
                    try {
                        fos = new FileOutputStream(destFile);
                    }

                    catch (FileNotFoundException e) {
                        jlog.fatal("File not found " + destFile.getName());
                    }

                    catch (SecurityException e) {
                        jlog.fatal("Access denied to " + destFile.getName());
                    }

                    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();
                    is.close();

                }
            }

            catch (IOException ioe) {
                jlog.fatal("IO exception in  " + zipFile.getName());
            }
        }
        try {
            zipFile.close();
        } catch (IOException e) {
            jlog.fatal("IO exception when closing  " + zipFile.getName());
        }
    }

    // Recursively decompress the list of zip files
    for (File f : zipFiles) {
        decompressZip(f, zipPath);
    }

    return;
}