Example usage for java.util.zip ZipInputStream available

List of usage examples for java.util.zip ZipInputStream available

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream available.

Prototype

public int available() throws IOException 

Source Link

Document

Returns 0 after EOF has reached for the current entry data, otherwise always return 1.

Usage

From source file:eionet.gdem.conversion.odf.OpenDocumentUtils.java

/**
 * Returns true, if inputstream is zip file
 * @param input InputStream//from  w  w w.  j  av a2 s . co m
 * @return True if InputStream is a zip file.
 */
public static boolean isSpreadsheetFile(InputStream input) {

    ZipInputStream zipStream = null;
    ZipEntry zipEntry = null;
    try {
        zipStream = new ZipInputStream(input);
        while (zipStream.available() == 1 && (zipEntry = zipStream.getNextEntry()) != null) {
            if (zipEntry != null) {
                if ("content.xml".equals(zipEntry.getName())) {
                    // content file found, it is OpenDocument.
                    return true;
                }
            }
        }
    } catch (IOException ioe) {
        return false;
    } finally {
        IOUtils.closeQuietly(zipStream);
    }
    return false;

}

From source file:org.adl.samplerte.server.LMSPackageHandler.java

/****************************************************************************
**
** Method:   findManifest()/*from   w ww.ja  v  a2s. c  om*/
** Input:  String zipFileName  --  The name of the zip file to be used
** Output:   Boolean  --  Signifies whether or not the manifest was found.
**
** Description: This method takes in the name of a zip file and tries to 
**              locate the imsmanifest.xml file
**              
*****************************************************************************/
public static boolean findManifest(String zipFileName) {
    if (_Debug) {
        System.out.println("***********************");
        System.out.println("in findManifest()      ");
        System.out.println("***********************\n");
    }

    boolean rtn = false;

    try {
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));

        ZipEntry entry;
        int flag = 0;

        while ((flag != 1) && (in.available() != 0)) {
            entry = in.getNextEntry();

            if (in.available() != 0) {
                if ((entry.getName()).equalsIgnoreCase("imsmanifest.xml")) {
                    if (_Debug) {
                        System.out.println("Located manifest.... returning true");
                    }
                    flag = 1;
                    rtn = true;
                }
            }
        }

        in.close();
    } catch (IOException e) {
        if (_Debug) {
            System.out.println("IO Exception Caught: " + e);
        }
    }
    return rtn;
}

From source file:org.adl.samplerte.server.LMSPackageHandler.java

/****************************************************************************
 **//from w  w  w . j  a  v a 2s .  c  o m
 ** Method:   findMetadata()
 ** Input:  String zipFileName  --  The name of the zip file to be used
 ** Output: Boolean  --  Whether or not any xml files were found  
 **
 ** Description: This method takes in the name of a zip file and locates 
 **              all files with an .xml extension 
 **              
 *****************************************************************************/
public static boolean findMetadata(String zipFileName) {
    if (_Debug) {
        System.out.println("***********************");
        System.out.println("in findMetadata()      ");
        System.out.println("***********************\n");
    }

    boolean rtn = false;
    String suffix = ".xml";

    try {
        //  The zip file being searched.
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
        //  An entry in the zip file
        ZipEntry entry;

        while ((in.available() != 0)) {
            entry = in.getNextEntry();

            if (in.available() != 0) {
                if ((entry.getName()).endsWith(suffix)) {
                    rtn = true;
                    if (_Debug) {
                        System.out.println("Other Meta-data located... returning true");
                    }
                }
            }
        }

        in.close();
    } catch (IOException e) {
        if (_Debug) {
            System.out.println("IO Exception Caught: " + e);
        }
    }

    return rtn;
}

From source file:org.adl.samplerte.server.LMSPackageHandler.java

/****************************************************************************
 **/*from  w w  w.  j ava 2  s  . com*/
 ** Method:   locateMetadata()
 ** Input:  String zipFileName  --  The name of the zip file to be used
 ** Output: Vector  --  A vector of the names of xml files.  
 **
 ** Description: This method takes in the name of a zip file and locates 
 **              all files with an .xml extension an adds their names to a 
 **              vector. 
 **              
 *****************************************************************************/
public static Vector locateMetadata(String zipFileName) {
    if (_Debug) {
        System.out.println("***********************");
        System.out.println("in locateMetadata()    ");
        System.out.println("***********************\n");
    }

    //  An array of names of xml files to be returned to ColdFusion
    Vector metaDataVector = new Vector();
    String suffix = ".xml";

    try {
        //  The zip file being searched.
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
        //  An entry in the zip file
        ZipEntry entry;

        if (_Debug) {
            System.out.println("Other meta-data located:");
        }
        while ((in.available() != 0)) {
            entry = in.getNextEntry();

            if (in.available() != 0) {
                if ((entry.getName()).endsWith(suffix)) {
                    if (_Debug) {
                        System.out.println(entry.getName());
                    }
                    metaDataVector.addElement(entry.getName());
                }
            }
        }
        in.close();

    } catch (IOException e) {
        if (_Debug) {
            System.out.println("IO Exception Caught: " + e);
        }
    }
    return metaDataVector;
}

From source file:com.celements.photo.utilities.Unzip.java

/**
 * Get a List of names of all files contained in the zip file.
 * //from  w  w w .  j  a v  a  2  s.co  m
 * @param zipFile byte array of the zip file.
 * @return List of all filenames (and directory names - ending with a file seperator) contained in the zip file.
 */
public List<String> getZipContentList(byte[] zipFile) {
    String fileSep = System.getProperty("file.separator");
    List<String> contentList = new ArrayList<String>();
    ZipInputStream zipStream = getZipInputStream(zipFile);

    try {
        while (zipStream.available() > 0) {
            ZipEntry entry = zipStream.getNextEntry();
            if (entry != null) {
                String fileName = entry.getName();
                if (entry.isDirectory() && !fileName.endsWith(fileSep)) {
                    fileName += fileSep;
                }
                contentList.add(fileName);
            }
        }
    } catch (IOException e) {
        mLogger.error(e);
    }

    return contentList;
}

From source file:com.celements.photo.utilities.Unzip.java

private ByteArrayOutputStream findAndExtractFile(String filename, ZipInputStream zipIn) throws IOException {
    ByteArrayOutputStream out = null;

    for (ZipEntry entry = zipIn.getNextEntry(); zipIn.available() > 0; entry = zipIn.getNextEntry()) {
        if (!entry.isDirectory() && entry.getName().equals(filename)) {
            // read the data and write it to the OutputStream
            int count;
            byte[] data = new byte[BUFFER];
            out = new ByteArrayOutputStream();
            BufferedOutputStream byteOut = new BufferedOutputStream(out, BUFFER);
            while ((count = zipIn.read(data, 0, BUFFER)) != -1) {
                byteOut.write(data, 0, count);
            }//from w  w  w . j  a  va  2 s. com
            byteOut.flush();
            break;
        }
    }

    zipIn.close();
    return out;
}

From source file:edu.ku.brc.helpers.ZipFileHelper.java

/**
 * @param zipFile//from  w  w  w  . j av  a  2  s.c  o m
 * @return
 * @throws ZipException
 * @throws IOException
 */
public List<File> unzipToFiles(final File zipFile) throws ZipException, IOException {
    Vector<File> files = new Vector<File>();

    final int bufSize = 65535;

    File dir = UIRegistry.getAppDataSubDir(Long.toString(System.currentTimeMillis()) + "_zip", true);
    dirsToRemoveList.add(dir);

    File outFile = null;
    FileOutputStream fos = null;
    try {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry = zin.getNextEntry();
        while (entry != null) {
            if (zin.available() > 0) {
                outFile = new File(dir.getCanonicalPath() + File.separator + entry.getName());
                fos = new FileOutputStream(outFile);

                byte[] bytes = new byte[bufSize]; // 64k
                int bytesRead = zin.read(bytes, 0, bufSize);
                while (bytesRead > 0) {
                    fos.write(bytes, 0, bytesRead);
                    bytesRead = zin.read(bytes, 0, bufSize);
                }

                files.insertElementAt(outFile.getCanonicalFile(), 0);
            }
            entry = zin.getNextEntry();
        }

    } finally {
        if (fos != null) {
            fos.close();
        }
    }
    return files;
}

From source file:edu.ku.brc.helpers.ZipFileHelper.java

/**
 * Unzips a a zip file cntaining just one file.
 * @param zipFile the backup file//from   w  ww  .  j a  v  a2  s  . co  m
 * @return the file of the new uncompressed back up file.
 */
public File unzipToSingleFile(final File zipFile) {
    final int bufSize = 102400;

    File outFile = null;
    try {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry = zin.getNextEntry();
        if (entry == null) {
            return null;
        }
        if (zin.available() == 0) {
            return null;
        }
        outFile = File.createTempFile("zip_", "sql");
        FileOutputStream fos = new FileOutputStream(outFile);

        byte[] bytes = new byte[bufSize]; // 10K
        int bytesRead = zin.read(bytes, 0, bufSize);
        while (bytesRead > 0) {
            fos.write(bytes, 0, bytesRead);
            bytesRead = zin.read(bytes, 0, 100);
        }

    } catch (ZipException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ZipFileHelper.class, ex);
        return null; //I think this means it is not a zip file.

    } catch (Exception ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ZipFileHelper.class, ex);
        return null;
    }
    return outFile;
}

From source file:eionet.gdem.conversion.odf.ODFSpreadsheetAnalyzer.java

/**
 * Analyze the content file in a <code>File</code> which is a .zip file.
 *
 * @param inputStream/*from   w w w . jav a  2  s .c  o m*/
 *            a <code>File</code> that contains OpenDocument content-information information.
 */
public OpenDocumentSpreadsheet analyzeZip(InputStream inputStream) {
    OpenDocumentSpreadsheet spreadsheet = null;
    ZipInputStream zipStream = null;
    try {
        zipStream = new ZipInputStream(inputStream);
        while (zipStream.available() == 1) {
            // read possible contentEntry
            ZipEntry cententEntry = zipStream.getNextEntry();
            if (cententEntry != null) {
                if ("content.xml".equals(cententEntry.getName())) {
                    // if real contentEntry we use content to do real
                    // analysis
                    spreadsheet = analyzeSpreadsheet(zipStream);
                    // analyze is made and we can break the loop
                    break;
                }
            }
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        IOUtils.closeQuietly(zipStream);
    }
    return spreadsheet;
}

From source file:com.thoughtworks.go.server.service.BackupServiceIntegrationTest.java

private String fileContents(File location, String filename) throws IOException {
    ZipInputStream zipIn = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*from  w  w w.ja v  a 2  s .c o  m*/
        zipIn = new ZipInputStream(new FileInputStream(location));
        while (zipIn.available() > 0) {
            ZipEntry nextEntry = zipIn.getNextEntry();
            if (nextEntry.getName().equals(filename)) {
                IOUtils.copy(zipIn, out);
            }
        }
    } finally {
        if (zipIn != null) {
            zipIn.close();
        }
    }
    return out.toString();
}