Search in sources :

Example 61 with ZipInputStream

use of java.util.zip.ZipInputStream in project platform_frameworks_base by android.

the class BugreportReceiverTest method assertZipContent.

private void assertZipContent(Uri uri, String entryName, String expectedContent) throws IOException, IOException {
    Log.v(TAG, "assertZipEntry(uri=" + uri + ", entryName=" + entryName);
    try (ZipInputStream zis = new ZipInputStream(mContext.getContentResolver().openInputStream(uri))) {
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            Log.v(TAG, "Zip entry: " + entry.getName());
            if (entry.getName().equals(entryName)) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                Streams.copy(zis, bos);
                String actualContent = new String(bos.toByteArray(), "UTF-8");
                bos.close();
                assertEquals("wrong content for zip entry'" + entryName + "' on '" + uri + "'", expectedContent, actualContent);
                return;
            }
        }
    }
    fail("Did not find entry '" + entryName + "' on file '" + uri + "'");
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 62 with ZipInputStream

use of java.util.zip.ZipInputStream in project translationstudio8 by heartsome.

the class ZipUtil method upZipFile.

/**
	 * 将压缩文件中的内容解压到指定目录中<br>
	 * 如果<code>baseDir</code>的值为空,则将文件解压到相同的目录中,目录名称为"zipFile_files"
	 * @param zipFile
	 *            压缩文件路径
	 * @param baseDir
	 *            解压的目标路径,可以为null
	 * @throws IOException
	 */
public static String upZipFile(String zipFile, String baseDir) throws IOException {
    File f = new File(zipFile);
    if (baseDir == null) {
        baseDir = f.getPath() + "_files";
    }
    ZipInputStream zis = new ZipInputStream(new FileInputStream(f));
    ZipEntry ze;
    byte[] buf = new byte[1024];
    while ((ze = zis.getNextEntry()) != null) {
        File outFile = getRealFileName(baseDir, ze.getName());
        FileOutputStream os = new FileOutputStream(outFile);
        int readLen = 0;
        while ((readLen = zis.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        os.close();
    }
    zis.close();
    return baseDir;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 63 with ZipInputStream

use of java.util.zip.ZipInputStream in project h2o-3 by h2oai.

the class GlmMojoBenchHelper method readData.

static void readData(File f, int[] mapping, String firstColName, double[][] out, MojoModel mojo) throws IOException {
    InputStream is = new FileInputStream(f);
    try {
        InputStream source;
        if (f.getName().endsWith(".zip")) {
            ZipInputStream zis = new ZipInputStream(is);
            ZipEntry entry = zis.getNextEntry();
            if (!entry.getName().endsWith(".csv"))
                throw new IllegalStateException("CSV file expected, name " + entry.getName());
            source = zis;
        } else {
            source = new GZIPInputStream(is);
        }
        CSVReader r = new CSVReader(new InputStreamReader(source));
        if (firstColName != null) {
            String[] header = r.readNext();
            if (header == null)
                throw new IllegalStateException("File empty");
            if (!firstColName.equals(header[0]))
                throw new IllegalStateException("Header expected");
        }
        int rowIdx = 0;
        String[] row;
        while ((rowIdx < out.length) && ((row = r.readNext()) != null)) {
            double[] outRow = out[rowIdx++];
            if (row.length < mapping.length)
                throw new IllegalStateException("Row too short: " + Arrays.toString(row));
            for (int i = 0; i < mapping.length; i++) {
                int target = mapping[i];
                if (target < 0)
                    continue;
                if ("NA".equals(row[i])) {
                    outRow[target] = Double.NaN;
                    continue;
                }
                String[] domain = mojo.getDomainValues(target);
                if (domain == null)
                    outRow[target] = Double.parseDouble(row[i]);
                else {
                    outRow[target] = -1;
                    for (int d = 0; d < domain.length; d++) if (domain[d].equals(row[i])) {
                        outRow[target] = d;
                        break;
                    }
                    if (outRow[target] < 0)
                        throw new IllegalStateException("Value " + row[i] + " not found in domain " + Arrays.toString(domain));
                }
            }
        }
    } finally {
        is.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) CSVReader(au.com.bytecode.opencsv.CSVReader) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry)

Example 64 with ZipInputStream

use of java.util.zip.ZipInputStream in project h2o-3 by h2oai.

the class MojoReaderBackendFactory method createInMemoryReaderBackend.

private static MojoReaderBackend createInMemoryReaderBackend(InputStream inputStream) throws IOException {
    HashMap<String, byte[]> content = new HashMap<>();
    ZipInputStream zis = new ZipInputStream(inputStream);
    try {
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getSize() > Integer.MAX_VALUE)
                throw new IOException("File too large: " + entry.getName());
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            copyStream(zis, os);
            content.put(entry.getName(), os.toByteArray());
        }
        zis.close();
    } finally {
        closeQuietly(zis);
    }
    return new InMemoryMojoReaderBackend(content);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry)

Example 65 with ZipInputStream

use of java.util.zip.ZipInputStream in project h2o-3 by h2oai.

the class ZipUtil method unzipForHeader.

/**
   * This method will read a compressed zip file and return the uncompressed bits so that we can
   * check the beginning of the file and make sure it does not contain the column names.
   *
   * @param bs
   * @param chkSize
   * @return
   */
static byte[] unzipForHeader(byte[] bs, int chkSize) {
    ByteArrayInputStream bais = new ByteArrayInputStream(bs);
    ZipInputStream zis = new ZipInputStream(bais);
    InputStream is = zis;
    // Now read from the compressed stream
    int off = 0;
    try {
        while (off < bs.length) {
            int len = 0;
            len = is.read(bs, off, bs.length - off);
            if (len < 0)
                break;
            off += len;
            if (off == bs.length) {
                // Dataset is uncompressing alot! Need more space...
                if (bs.length >= chkSize)
                    // Already got enough
                    break;
                bs = Arrays.copyOf(bs, bs.length * 2);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bs;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)968 ZipEntry (java.util.zip.ZipEntry)762 IOException (java.io.IOException)355 File (java.io.File)319 FileInputStream (java.io.FileInputStream)316 InputStream (java.io.InputStream)203 FileOutputStream (java.io.FileOutputStream)198 ByteArrayInputStream (java.io.ByteArrayInputStream)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)138 BufferedInputStream (java.io.BufferedInputStream)127 ZipOutputStream (java.util.zip.ZipOutputStream)91 Test (org.junit.Test)89 ArrayList (java.util.ArrayList)80 OutputStream (java.io.OutputStream)67 URL (java.net.URL)58 Path (java.nio.file.Path)58 FileNotFoundException (java.io.FileNotFoundException)56 HashMap (java.util.HashMap)56 BufferedOutputStream (java.io.BufferedOutputStream)54 ZipFile (java.util.zip.ZipFile)43