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 + "'");
}
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;
}
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();
}
}
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);
}
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;
}
Aggregations