Example usage for java.util.zip ZipInputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for reading the next entry.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream("C:/MyZip.zip");
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze;//from   w  w w  . j  ava 2s. co  m
    while ((ze = zis.getNextEntry()) != null) {
        System.out.println(ze.getName());
        zis.closeEntry();
    }

    zis.close();

}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    for (int i = 0; i < args.length; i++) {
        FileInputStream fin = new FileInputStream(args[i]);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;//from ww  w  .ja  v a 2 s  . com
        while ((ze = zin.getNextEntry()) != null) {
            System.out.println("Unzipping " + ze.getName());
            FileOutputStream fout = new FileOutputStream(ze.getName());
            for (int c = zin.read(); c != -1; c = zin.read()) {
                fout.write(c);
            }
            zin.closeEntry();
            fout.close();
        }
        zin.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream("filename"));
    ZipEntry zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        String entryName = zipentry.getName();
        File newFile = new File(entryName);
        String directory = newFile.getParent();
        if (directory == null) {
            if (newFile.isDirectory())
                break;
        }/* w  ww.j  a  va2  s . com*/
        RandomAccessFile rf = new RandomAccessFile(entryName, "r");
        String line;
        if ((line = rf.readLine()) != null) {
            System.out.println(line);
        }
        rf.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();
    }
    zipinputstream.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String destinationname = "d:\\";
    byte[] buf = new byte[1024];
    ZipInputStream zipinputstream = null;
    ZipEntry zipentry;/*  w  w w . j  av a 2s. c o  m*/
    zipinputstream = new ZipInputStream(new FileInputStream("fileName"));
    zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        String entryName = zipentry.getName();
        FileOutputStream fileoutputstream;
        File newFile = new File(entryName);
        String directory = newFile.getParent();

        if (directory == null) {
            if (newFile.isDirectory())
                break;
        }
        fileoutputstream = new FileOutputStream(destinationname + entryName);
        int n;
        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            fileoutputstream.write(buf, 0, n);
        }
        fileoutputstream.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();
    }
    zipinputstream.close();
}

From source file:com.wisemapping.util.ZipUtils.java

public static byte[] zipToBytes(byte[] zip) throws IOException {

    byte[] result = null;
    if (zip != null) {
        final ByteArrayInputStream in = new ByteArrayInputStream(zip);
        final ZipInputStream zipIn = new ZipInputStream(in);
        zipIn.getNextEntry();//from w w w.  ja v  a2s.  c om
        result = IOUtils.toByteArray(zipIn);

        zipIn.closeEntry();
        zipIn.close();
    }

    return result;
}

From source file:org.opentestsystem.authoring.testitembank.service.impl.ApipZipOutputFileBuilderService.java

private static final void closeEntryQuiety(final ZipInputStream zis) {
    try {/*w w w. ja v a 2 s. c o m*/
        if (zis != null) {
            zis.closeEntry();
        }
    } catch (final IOException e) {
        LOGGER.error("error closing zip input stream entry.", e);
    }
}

From source file:org.exist.util.Compressor.java

public static void uncompress(byte[] whatToUncompress, OutputStream os) throws IOException {
    final ByteArrayInputStream bais = new ByteArrayInputStream(whatToUncompress);
    final ZipInputStream gzis = new ZipInputStream(bais);
    final ZipEntry zipentry = gzis.getNextEntry();
    Integer.parseInt(zipentry.getName());
    final byte[] buf = new byte[512];
    int bread;//w  w  w.  ja v  a2s  .  co m
    while ((bread = gzis.read(buf)) != -1)
        os.write(buf, 0, bread);
    gzis.closeEntry();
    gzis.close();
}

From source file:org.silverpeas.file.ZipUtil.java

/**
 * ---------------------------------------------------------------------
 * @param from/*from  w w w.  j  a va2s  .  c om*/
 * @param to
 * @throws IOException
 * @throws IllegalArgumentException
 * @see
 */
public static void unzip(final File fromZipFile, final File toDir) throws IOException {
    if (!toDir.exists()) {
        toDir.mkdirs();
    }
    final ZipFile zipFile = new ZipFile(fromZipFile);
    ZipInputStream zipInput = null;
    try {
        zipInput = new ZipInputStream(new FileInputStream(fromZipFile));
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            extractFile(zipFile, entry, toDir);
            zipInput.closeEntry();
        }
    } finally {
        if (zipInput != null) {
            IOUtils.closeQuietly(zipInput);
        }
        zipFile.close();
    }
}

From source file:Main.java

private static File unzip(Context context, InputStream in) throws IOException {
    File tmpDb = null;/*from   ww w  .ja  v  a 2 s .  c  o m*/
    int dbFiles = 0;
    try {
        tmpDb = File.createTempFile("import", ".db");

        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry sourceEntry;
        while (true) {

            sourceEntry = zin.getNextEntry();

            if (sourceEntry == null) {
                break;
            }

            if (sourceEntry.isDirectory()) {
                zin.closeEntry();
                continue;
            }

            FileOutputStream fOut;
            if (sourceEntry.getName().endsWith(".db")) {
                // Write database to tmp file
                fOut = new FileOutputStream(tmpDb);
                dbFiles++;
            } else {
                // Write all other files(images) to files dir in apps data
                int start = sourceEntry.getName().lastIndexOf("/") + 1;
                String name = sourceEntry.getName().substring(start);
                fOut = context.openFileOutput(name, Context.MODE_PRIVATE);
            }

            final OutputStream targetStream = fOut;
            try {
                int read;
                while (true) {
                    byte[] buffer = new byte[1024];
                    read = zin.read(buffer);
                    if (read == -1) {
                        break;
                    }
                    targetStream.write(buffer, 0, read);
                }
                targetStream.flush();
            } finally {
                safeCloseClosable(targetStream);
            }
            zin.closeEntry();
        }
    } finally {
        safeCloseClosable(in);
    }
    if (dbFiles != 1) {
        throw new IllegalStateException("Input file is not a valid backup");
    }
    return tmpDb;
}

From source file:Main.java

/**
 * Decompresses a given byte array that is a compressed folder.
 * //from  w ww.  j a v a2 s  .c o  m
 * @param folderAsCompressedArray to decompress
 * @param unzippedLocation where the decompressed folder should be
 * @throws IOException e
 * @throws FileNotFoundException e
 */
public static void decompressFolderByteArray(byte[] folderAsCompressedArray, File unzippedLocation)
        throws IOException, FileNotFoundException {
    ZipInputStream zipFile = new ZipInputStream(new ByteArrayInputStream(folderAsCompressedArray));
    ZipEntry ze = null;
    final int minusOne = -1;
    while ((ze = zipFile.getNextEntry()) != null) {
        FileOutputStream fout = new FileOutputStream(
                new File(unzippedLocation, ze.getName()).getAbsolutePath());
        for (int c = zipFile.read(); c != minusOne; c = zipFile.read()) {
            fout.write(c);
        }
        zipFile.closeEntry();
        fout.close();
    }
    zipFile.close();
}