private long writeFile(BufferedInputStream in, ZipOutputStream out, //ZipArchiveOutputStream out, ProgressObserver observer, long bytesSoFar, long totalSize) throws IOException { long totalBytesWritten = bytesSoFar; int bytesIn = 0; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesIn = in.read(buffer)) != -1) { totalBytesWritten += bytesIn; int progressSoFar = (int) ((UNITY_PERCENT * totalBytesWritten) / totalSize); observer.onProgress(progressSoFar); out.write(buffer, 0, bytesIn); } return totalBytesWritten; } }
private long writeFile(final InputStream in, final ZipOutputStream out, //ZipArchiveOutputStream out, final ProgressObserver observer, final long bytesSoFar, final long totalSize) throws IOException { long totalBytesWritten = bytesSoFar; int bytesIn = 0; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesIn = in.read(buffer)) != -1) { totalBytesWritten += bytesIn; int progressSoFar = (int) ((UNITY_PERCENT * totalBytesWritten) / totalSize); observer.onProgress(progressSoFar); out.write(buffer, 0, bytesIn); } return totalBytesWritten; } }
@Test public final void testGoodGetCheckedInputStream() throws IOException { // Create test ZIP file. final String name = "entry"; final ZipOutputStream zipOut = newZipOutputStream(new FileOutputStream(file)); zipOut.putNextEntry(newEntry(name)); zipOut.write(data); zipOut.close(); final ZipFile zipIn = newZipFile(file); // Open checked input stream and join immediately. InputStream in = zipIn.getCheckedInputStream(name); in.close(); // Open checked input stream and read fully, using multiple methods. in = zipIn.getCheckedInputStream(name); final int n = data.length / 4; in.skip(n); in.read(new byte[n]); in.read(new byte[n], 0, n); while (in.read() != -1) { // read until EOF } in.close(); zipIn.close(); }
private void append( final int off, final int len, final byte[] data) throws IOException { final ZipOutputStream out; if (file.exists()) { final ZipFile in = newZipFile(file); in.close(); out = newZipOutputStream(new FileOutputStream(file, true), in); } else { out = newZipOutputStream(new FileOutputStream(file)); } try { for (int i = 0; i < len; i++) { final String name = off + i + ".txt"; out.putNextEntry(newEntry(name)); out.write(data); } } finally { out.close(); } } }
@Test public final void testWriteAndReadSingleBytes() throws IOException { final ZipOutputStream zipOut = newZipOutputStream(new FileOutputStream(file)); zipOut.putNextEntry(newEntry("file")); for (int i = 0; i < data.length; i++) zipOut.write(data[i]); zipOut.close(); final ZipFile zipIn = newZipFile(file); InputStream in = zipIn.getInputStream("file"); for (int i = 0, c; (c = in.read()) != -1; i++) assertEquals(data[i] & 0xFF, c); in.close(); zipIn.close(); }
try { zipOut.putNextEntry(newEntry(name)); zipOut.write(data); } finally { zipOut.close();
String name = i + ".txt"; zout.putNextEntry(newEntry(name)); zout.write(data); assertTrue(set.add(name));
zos.write(data); assertTrue(set.add(name));