Search in sources :

Example 86 with ZipEntry

use of java.util.zip.ZipEntry in project OpenGrok by OpenGrok.

the class SCCSgetTest method getRevision.

/**
     * Test of getRevision method, of class SCCSget.
     */
@Test
public void getRevision() throws Exception {
    if (!haveSccs) {
        System.out.println("sccs not available. Skipping test");
        return;
    }
    ZipInputStream zstream = new ZipInputStream(getClass().getResourceAsStream("sccs-revisions.zip"));
    ZipEntry entry;
    while ((entry = zstream.getNextEntry()) != null) {
        String expected = readInput(zstream);
        InputStream sccs = SCCSget.getRevision("sccs", sccsfile, entry.getName());
        String got = readInput(sccs);
        sccs.close();
        zstream.closeEntry();
        assertEquals(expected, got);
    }
    zstream.close();
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Test(org.junit.Test)

Example 87 with ZipEntry

use of java.util.zip.ZipEntry in project OpenGrok by OpenGrok.

the class FileUtilities method extractArchive.

public static void extractArchive(File sourceBundle, File root) throws IOException {
    ZipFile zipfile = new ZipFile(sourceBundle);
    Enumeration<? extends ZipEntry> e = zipfile.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = e.nextElement();
        File file = new File(root, ze.getName());
        if (ze.isDirectory()) {
            file.mkdirs();
        } else {
            InputStream in = zipfile.getInputStream(ze);
            assertNotNull(in);
            FileOutputStream out = new FileOutputStream(file);
            assertNotNull(out);
            copyFile(in, out);
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 88 with ZipEntry

use of java.util.zip.ZipEntry in project OpenMEAP by OpenMEAP.

the class LocalStorageImpl method unzipImportArchive.

public void unzipImportArchive(UpdateStatus update) throws LocalStorageException {
    // at this point, we've verified that:
    //   1) we have enough space on the device
    //   2) the archive downloaded is what was expected
    ZipInputStream zis = null;
    String newPrefix = "com.openmeap.storage." + update.getUpdateHeader().getHash().getValue();
    File hashHolder = null;
    String hashRootAbsolutePath = "";
    try {
        hashHolder = new File(activity.getFilesDir(), newPrefix);
        hashHolder.mkdir();
        hashRootAbsolutePath = hashHolder.getAbsolutePath();
    } catch (Exception e) {
        System.out.println("Exception thrown while creating hash folder.");
        System.out.println(e);
    }
    try {
        zis = new ZipInputStream(getImportArchiveInputStream());
        ZipEntry ze;
        while ((ze = zis.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                //		    		continue;
                try {
                    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    System.out.println("Writing directory structure in phone memory.");
                    File directoryStructure = new File(hashRootAbsolutePath, ze.getName());
                    directoryStructure.mkdirs();
                } catch (Exception e) {
                    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    System.out.println("Exception thrown while writing directory structure.");
                    System.out.println(e);
                }
            } else {
                try {
                    String osSeperator = System.getProperty("file.separator");
                    int seperatorLastIndex = ze.getName().lastIndexOf(osSeperator);
                    String fileName = ze.getName().substring(seperatorLastIndex + 1, ze.getName().length());
                    String fileNameParentDirectoryPrefix = "";
                    String absolutePathFromPrefix = "";
                    if (seperatorLastIndex != -1 && seperatorLastIndex != 0) {
                        fileNameParentDirectoryPrefix = ze.getName().substring(0, seperatorLastIndex);
                        absolutePathFromPrefix = hashRootAbsolutePath + osSeperator + fileNameParentDirectoryPrefix;
                    } else {
                        absolutePathFromPrefix = hashRootAbsolutePath + osSeperator;
                    }
                    URI osResourePathForThisFile = URI.create(absolutePathFromPrefix);
                    File writableFileReference = new File(osResourePathForThisFile.getPath(), fileName);
                    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(writableFileReference.getAbsolutePath(), true), 1024);
                    try {
                        byte[] buffer = new byte[1024];
                        int count;
                        while ((count = zis.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, count);
                        }
                    } catch (Exception e) {
                        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                        System.out.println("Exception while writing file contents.");
                        System.out.println(e);
                    } finally {
                        outputStream.close();
                    }
                } catch (Exception e) {
                    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
                    System.out.println("Unknown exception.");
                    System.out.println(e);
                }
            }
        //		    	Commenting following code to make use of file:/// alternate to content://
        //		        OutputStream baos = openFileOutputStream(newPrefix,ze.getName());		        
        //		        try {
        //		        	byte[] buffer = new byte[1024];
        //		        	int count;
        //		        	while ((count = zis.read(buffer)) != -1) {
        //		        		baos.write(buffer, 0, count);
        //		        	}
        //		        }
        //		        catch( Exception e ) {
        //		        	;// TODO: something, for the love of god.
        //		        }
        //		        finally {
        //		        	baos.close();
        //		        }
        }
    } catch (Exception e) {
        throw new LocalStorageException(e);
    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                throw new GenericRuntimeException(e);
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) URI(java.net.URI) LocalStorageException(com.openmeap.thinclient.LocalStorageException) LocalStorageException(com.openmeap.thinclient.LocalStorageException) GenericRuntimeException(com.openmeap.util.GenericRuntimeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UpdateException(com.openmeap.thinclient.update.UpdateException) ZipInputStream(java.util.zip.ZipInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 89 with ZipEntry

use of java.util.zip.ZipEntry in project OpenMEAP by OpenMEAP.

the class FileOperationManagerImpl method unzipFile.

@Override
public void unzipFile(ZipFile zipFile, String destinationDir) throws FileOperationException {
    try {
        int BUFFER = 1024;
        BufferedOutputStream dest = null;
        BufferedInputStream is = null;
        OutputStream fos = null;
        ZipEntry entry;
        Enumeration e = zipFile.entries();
        while (e.hasMoreElements()) {
            try {
                entry = (ZipEntry) e.nextElement();
                is = new BufferedInputStream(zipFile.getInputStream(entry));
                String newFile = destinationDir + File.separator + entry.getName();
                if (entry.isDirectory()) {
                    // skip directories, resource manager will create for us
                    continue;
                } else {
                    fos = write(newFile);
                    dest = new BufferedOutputStream(fos, BUFFER);
                    Utils.pipeInputStreamIntoOutputStream(is, dest);
                }
            } finally {
                if (dest != null) {
                    dest.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (is != null) {
                    is.close();
                }
            }
        }
    } catch (IOException ioe) {
        throw new FileOperationException(ioe);
    }
}
Also used : Enumeration(java.util.Enumeration) BufferedInputStream(java.io.BufferedInputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 90 with ZipEntry

use of java.util.zip.ZipEntry in project OpenMEAP by OpenMEAP.

the class ZipUtils method unzipFile.

/**
	 * Unzips a file into the destination directory provided.
	 * 
	 * @param zipFile
	 * @param destinationDir
	 * @throws IOException
	 */
public static void unzipFile(ZipFile zipFile, File destinationDir) throws IOException {
    int BUFFER = 1024;
    BufferedOutputStream dest = null;
    BufferedInputStream is = null;
    if (!destinationDir.exists()) {
        destinationDir.mkdir();
    }
    ZipEntry entry;
    Enumeration e = zipFile.entries();
    while (e.hasMoreElements()) {
        try {
            entry = (ZipEntry) e.nextElement();
            is = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(destinationDir, entry.getName());
            if (!file.exists() && entry.isDirectory()) {
                file.mkdirs();
            } else {
                FileOutputStream fos = new FileOutputStream(file, false);
                dest = new BufferedOutputStream(fos, BUFFER);
                Utils.pipeInputStreamIntoOutputStream(is, fos);
            }
        } finally {
            if (dest != null) {
                dest.close();
            }
            if (is != null) {
                is.close();
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1367 ZipFile (java.util.zip.ZipFile)479 File (java.io.File)469 IOException (java.io.IOException)361 ZipOutputStream (java.util.zip.ZipOutputStream)321 ZipInputStream (java.util.zip.ZipInputStream)300 InputStream (java.io.InputStream)282 FileOutputStream (java.io.FileOutputStream)278 FileInputStream (java.io.FileInputStream)270 Test (org.junit.Test)124 BufferedInputStream (java.io.BufferedInputStream)122 JarFile (java.util.jar.JarFile)122 BufferedOutputStream (java.io.BufferedOutputStream)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)97 ArrayList (java.util.ArrayList)84 ByteArrayInputStream (java.io.ByteArrayInputStream)78 OutputStream (java.io.OutputStream)67 JarOutputStream (java.util.jar.JarOutputStream)59 Path (java.nio.file.Path)56 Enumeration (java.util.Enumeration)56