Guest User

Untitled

a guest
Jun 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. ZipInputStream zis = new ZipInputStream(inputStream);
  2. ZipEntry ze;
  3. long totalBytesRead = 0;
  4. while ((ze = zis.getNextEntry()) != null) {
  5. BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(ze.getName()));
  6. byte[] buffer = new byte[4096];
  7. int i;
  8. while ((i = zis.read(buffer)) != -1) {
  9. totalBytesRead+=i;
  10. outStream.write(buffer,0,i);
  11. }
  12. outStream.close();
  13. }
  14. inputStream.close();
  15.  
  16. import java.io.FilterInputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19.  
  20. /**
  21. *
  22. */
  23.  
  24. /**
  25. * @author clint
  26. *
  27. */
  28. public class ByteCountingInputStream extends FilterInputStream {
  29.  
  30. public int totalRead = 0;
  31.  
  32. /**
  33. * @param in
  34. */
  35. protected ByteCountingInputStream(InputStream in) {
  36. super(in);
  37. // TODO Auto-generated constructor stub
  38. }
  39.  
  40. /* (non-Javadoc)
  41. * @see java.io.FilterInputStream#read()
  42. */
  43. @Override
  44. public int read() throws IOException {
  45. int ret = super.read();
  46. totalRead++;
  47. return ret;
  48. }
  49.  
  50. /* (non-Javadoc)
  51. * @see java.io.FilterInputStream#read(byte[], int, int)
  52. */
  53. @Override
  54. public int read(byte[] b, int off, int len) throws IOException {
  55. int ret = super.read(b, off, len);
  56. totalRead += ret;
  57. return ret;
  58. }
  59.  
  60. /* (non-Javadoc)
  61. * @see java.io.FilterInputStream#read(byte[])
  62. */
  63. @Override
  64. public int read(byte[] b) throws IOException {
  65. int ret = super.read(b);
  66. totalRead += ret;
  67. return ret;
  68. }
  69.  
  70. /* (non-Javadoc)
  71. * @see java.io.FilterInputStream#skip(long)
  72. */
  73. @Override
  74. public long skip(long n) throws IOException {
  75. //What to do?
  76. return super.skip(n);
  77. }
  78.  
  79. /**
  80. * @return the totalRead
  81. */
  82. protected int getTotalRead() {
  83. return this.totalRead;
  84. }
  85.  
  86. }
  87.  
  88. ZipInputStream zis = new ZipInputStream(new ByteCountingInputStream(inputStream));
  89.  
  90. import java.io.FilterInputStream;
  91. import java.io.IOException;
  92. import java.io.InputStream;
  93.  
  94. public class CountingInputStream extends FilterInputStream {
  95.  
  96. private long totalBytes = 0;
  97.  
  98. protected CountingInputStream(InputStream in) {
  99. super(in);
  100. }
  101.  
  102. public int getTotalBytesRead() {
  103. return totalBytes;
  104. }
  105.  
  106. @Override
  107. public int read() throws IOException {
  108. int byteValue = super.read();
  109. if (byteValue != -1) totalBytes++;
  110. return byteValue;
  111. }
  112.  
  113. @Override
  114. public int read(byte[] b) throws IOException {
  115. int bytesRead = super.read(b);
  116. if (bytesRead != -1) totalBytes+=bytesRead;
  117. return bytesRead;
  118. }
  119.  
  120. @Override
  121. public int read(byte[] b, int off, int len) throws IOException {
  122. int bytesRead = super.read(b,off,len);
  123. if (bytesRead != -1) totalBytes+=bytesRead;
  124. return bytesRead;
  125. }
  126. }
Add Comment
Please, Sign In to add comment