Exemplo n.º 1
0
 private void setSizesAndOffsetFromZip64Extra(
     final ZipEntry ze, final OffsetEntry offset, final int diskStart) throws IOException {
   final Zip64ExtendedInformationExtraField z64 =
       (Zip64ExtendedInformationExtraField)
           ze.getExtraField(Zip64ExtendedInformationExtraField.HEADER_ID);
   if (z64 != null) {
     final boolean hasUncompressedSize = ze.getSize() == 4294967295L;
     final boolean hasCompressedSize = ze.getCompressedSize() == 4294967295L;
     final boolean hasRelativeHeaderOffset = offset.headerOffset == 4294967295L;
     z64.reparseCentralDirectoryData(
         hasUncompressedSize, hasCompressedSize, hasRelativeHeaderOffset, diskStart == 65535);
     if (hasUncompressedSize) {
       ze.setSize(z64.getSize().getLongValue());
     } else if (hasCompressedSize) {
       z64.setSize(new ZipEightByteInteger(ze.getSize()));
     }
     if (hasCompressedSize) {
       ze.setCompressedSize(z64.getCompressedSize().getLongValue());
     } else if (hasUncompressedSize) {
       z64.setCompressedSize(new ZipEightByteInteger(ze.getCompressedSize()));
     }
     if (hasRelativeHeaderOffset) {
       offset.headerOffset = z64.getRelativeHeaderOffset().getLongValue();
     }
   }
 }
  private void addDirectoryToZipEntryList(
      File directory, String currentPath, ImmutableMap.Builder<File, ZipEntry> zipEntriesBuilder)
      throws IOException {
    Preconditions.checkNotNull(currentPath);

    for (File inputFile : directory.listFiles()) {
      String childPath = currentPath + (currentPath.isEmpty() ? "" : "/") + inputFile.getName();

      if (inputFile.isDirectory()) {
        addDirectoryToZipEntryList(inputFile, childPath, zipEntriesBuilder);
      } else {
        ZipEntry nextEntry = new ZipEntry(childPath);
        long fileLength = inputFile.length();
        if (fileLength > maxDeflatedBytes
            || EXTENSIONS_NOT_TO_DEFLATE.contains(Files.getFileExtension(inputFile.getName()))) {
          nextEntry.setMethod(ZipEntry.STORED);
          nextEntry.setCompressedSize(inputFile.length());
          nextEntry.setSize(inputFile.length());
          HashCode crc = ByteStreams.hash(Files.newInputStreamSupplier(inputFile), Hashing.crc32());
          nextEntry.setCrc(crc.padToLong());
        }

        zipEntriesBuilder.put(inputFile, nextEntry);
      }
    }
  }
Exemplo n.º 3
0
 private void readDataDescr() throws IOException {
   if (readLeInt() != EXTSIG) throw new ZipException("Data descriptor signature not found");
   entry.setCrc(readLeInt() & 0xffffffffL);
   csize = readLeInt();
   size = readLeInt();
   entry.setSize(size & 0xffffffffL);
   entry.setCompressedSize(csize & 0xffffffffL);
 }
Exemplo n.º 4
0
  /**
   * Open the next entry from the zip archive, and return its description. If the previous entry
   * wasn't closed, this method will close it.
   */
  public ZipEntry getNextEntry() throws IOException {
    if (crc == null) throw new IOException("Stream closed.");
    if (entry != null) closeEntry();

    int header = readLeInt();
    if (header == CENSIG) {
      /* Central Header reached. */
      close();
      return null;
    }
    if (header != LOCSIG)
      throw new ZipException("Wrong Local header signature: " + Integer.toHexString(header));
    /* skip version */
    readLeShort();
    flags = readLeShort();
    method = readLeShort();
    int dostime = readLeInt();
    int crc = readLeInt();
    csize = readLeInt();
    size = readLeInt();
    int nameLen = readLeShort();
    int extraLen = readLeShort();

    if (method == ZipOutputStream.STORED && csize != size)
      throw new ZipException("Stored, but compressed != uncompressed");

    byte[] buffer = new byte[nameLen];
    readFully(buffer);
    String name;
    try {
      name = new String(buffer, "UTF-8");
    } catch (UnsupportedEncodingException uee) {
      throw new AssertionError(uee);
    }

    entry = createZipEntry(name);
    entryAtEOF = false;
    entry.setMethod(method);
    if ((flags & 8) == 0) {
      entry.setCrc(crc & 0xffffffffL);
      entry.setSize(size & 0xffffffffL);
      entry.setCompressedSize(csize & 0xffffffffL);
    }
    entry.setDOSTime(dostime);
    if (extraLen > 0) {
      byte[] extra = new byte[extraLen];
      readFully(extra);
      entry.setExtra(extra);
    }

    if (method == ZipOutputStream.DEFLATED && avail > 0) {
      System.arraycopy(buf, len - avail, buf, 0, avail);
      len = avail;
      avail = 0;
      inf.setInput(buf, 0, len);
    }
    return entry;
  }
Exemplo n.º 5
0
  public AesZipEntry(ZipEntry zipEntry) {
    super(zipEntry.getName());
    super.setMethod(zipEntry.getMethod());
    super.setSize(zipEntry.getSize());
    super.setCompressedSize(zipEntry.getCompressedSize() + 28);
    super.setTime(zipEntry.getTime());

    flag |= 1; // bit0 - encrypted
    // flag |= 8;  // bit3 - use data descriptor
  }
Exemplo n.º 6
0
 /**
  * Warning this method is not thread safe!
  *
  * @param name file name including path in the zip
  * @param data
  * @throws IOException
  */
 public void writeStoredEntry(String name, byte[] data) throws IOException {
   ZipEntry ze = new ZipEntry(name);
   ze.setMethod(ZipEntry.STORED);
   ze.setCompressedSize(data.length);
   ze.setSize(data.length);
   crc.reset();
   crc.update(data);
   ze.setCrc(crc.getValue());
   putNextEntry(ze);
   write(data);
   closeEntry();
 }
 private static void writeMimeType(ZipOutputStream zip) throws IOException {
   byte[] content = EPUB_MIME_CONTENT.getBytes(UTF8_CHARSET);
   ZipEntry entry = new ZipEntry(MIMETYPE);
   entry.setMethod(ZipEntry.STORED);
   entry.setSize(20);
   entry.setCompressedSize(20);
   entry.setCrc(0x2CAB616F); // pre-computed
   zip.putNextEntry(entry);
   zip.write(content);
   zip.closeEntry();
   LOGGER.debug("Successfully wrote mimetype");
 }
  /** {@inheritDoc} */
  @Override
  public final void close() {
    if (this.readZipFile != null) {
      try {
        this.readZipFile.close();
        this.readZipFile = null;
      } catch (final IOException e) {
        throw new BufferedDataError(e);
      }
    }

    if (this.zos != null) {
      try {
        final ZipEntry theEntry = new ZipEntry("xl/worksheets/sheet1.xml");
        this.xmlOut.endTag();
        this.xmlOut.addAttribute("left", "0.7");
        this.xmlOut.addAttribute("right", "0.7");
        this.xmlOut.addAttribute("top", "0.75");
        this.xmlOut.addAttribute("bottom", "0.75");
        this.xmlOut.addAttribute("header", "0.3");
        this.xmlOut.addAttribute("footer", "0.3");

        this.xmlOut.beginTag("pageMargins");
        this.xmlOut.endTag();
        this.xmlOut.endTag();
        this.xmlOut.endDocument();

        final byte[] b = this.buffer.toByteArray();
        theEntry.setSize(b.length);
        theEntry.setCompressedSize(-1);
        theEntry.setMethod(ZipEntry.DEFLATED);
        this.zos.putNextEntry(theEntry);
        this.zos.write(b);
        this.zos.closeEntry();
        this.zos.close();
        this.zos = null;
      } catch (final IOException e) {
        throw new BufferedDataError(e);
      }
    }

    if (this.fos != null) {
      try {
        this.fos.close();
        this.fos = null;
      } catch (final IOException e) {
        throw new BufferedDataError(e);
      }
    }
  }
Exemplo n.º 9
0
 public ArrayList<ZipEntry> getArchiveItems(String zipFileName) {
   final int itemsPerEntry = 2;
   String[] in;
   synchronized (this) {
     in = getArchiveItemsInternal(zipFileName);
   }
   ArrayList<ZipEntry> list = new ArrayList<ZipEntry>();
   for (int i = 0; i <= in.length - itemsPerEntry; i += itemsPerEntry) {
     ZipEntry e = new ZipEntry(in[i]);
     e.setSize(Integer.valueOf(in[i + 1]));
     e.setCompressedSize(Integer.valueOf(in[i + 1]));
     list.add(e);
   }
   return list;
 }
Exemplo n.º 10
0
 public File call() throws IOException {
   System.err.println(Thread.currentThread() + ":instrumenting " + path);
   String prefix = path.substring(0, path.length() - 4);
   int slash = prefix.lastIndexOf("/");
   if (slash != -1) prefix = prefix.substring(slash + 1);
   prefix += "-";
   File zipTmp =
       File.createTempFile("instrumented-" + prefix, path.substring(path.length() - 4));
   try {
     zipTmp.deleteOnExit();
     ZipInputStream zin = new ZipInputStream(new FileInputStream(file));
     ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zipTmp));
     ZipEntry entry;
     while ((entry = zin.getNextEntry()) != null) {
       InputStream in;
       long size;
       if (entry.getName().endsWith(".class")) {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         copy(zin, baos);
         byte[] bytes = instrumenter.instrumentClass(baos.toByteArray());
         size = bytes.length;
         in = new ByteArrayInputStream(bytes);
       } else {
         in = zin;
         size = entry.getSize();
       }
       ZipEntry newEntry = new ZipEntry(entry);
       newEntry.setSize(size);
       newEntry.setCompressedSize(-1);
       zout.putNextEntry(newEntry);
       copy(in, zout);
       if (entry.getName().endsWith(".class")) {
         in.close();
       }
     }
     zout.close();
     return zipTmp;
   } catch (IOException e) {
     zipTmp.delete();
     throw e;
   }
 }
Exemplo n.º 11
0
  /** {@inheritDoc} */
  @Override
  public final void prepareWrite(final int recordCount, final int inputSize, final int idealSize) {
    this.inputCount = inputSize;
    this.idealCount = idealSize;
    ZipInputStream zis = null;

    try {
      this.fos = new FileOutputStream(this.file);
      this.zos = new ZipOutputStream(this.fos);

      final InputStream is =
          ResourceInputStream.openResourceInputStream("org/encog/data/blank.xlsx");

      zis = new ZipInputStream(is);

      ZipEntry theEntry;

      while (zis.available() > 0) {
        theEntry = zis.getNextEntry();
        if ((entry != null) && !"xl/worksheets/sheet1.xml".equals(entry.getName())) {

          final ZipEntry entry2 = new ZipEntry(theEntry);
          entry2.setCompressedSize(-1);
          this.zos.putNextEntry(entry2);
          final byte[] theBuffer = new byte[(int) entry.getSize()];
          zis.read(theBuffer);
          this.zos.write(theBuffer);
          this.zos.closeEntry();
        }
      }

      zis.close();
      zis = null;

      this.buffer = new ByteArrayOutputStream();
      this.xmlOut = new WriteXML(this.buffer);
      this.xmlOut.beginDocument();
      this.xmlOut.addAttribute(
          "xmlns", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
      this.xmlOut.addAttribute(
          "xmlns:r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
      this.xmlOut.beginTag("worksheet");
      final StringBuilder d = new StringBuilder();
      d.append(toColumn(this.inputCount + this.idealCount));
      d.append("" + recordCount);
      this.xmlOut.addAttribute("ref", "A1:" + d.toString());
      this.xmlOut.beginTag("dimension");
      this.xmlOut.endTag();
      this.xmlOut.beginTag("sheetViews");
      this.xmlOut.addAttribute("tabSelected", "1");
      this.xmlOut.addAttribute("workbookViewId", "0");
      this.xmlOut.beginTag("sheetView");
      this.xmlOut.endTag();
      this.xmlOut.endTag();
      this.xmlOut.addAttribute("defaultRowHeight", "15");
      this.xmlOut.beginTag("sheetFormatPtr");
      this.xmlOut.endTag();
      this.row = 1;
      this.xmlOut.beginTag("sheetData");

    } catch (final IOException ex) {
      throw new BufferedDataError(ex);
    } finally {
      if (zis != null) {
        try {
          zis.close();
        } catch (IOException e) {
          EncogLogging.log(e);
        }
      }
    }
  }
Exemplo n.º 12
0
  /**
   * compress all files with subdirs and content in a zip file
   *
   * @throws CompressionException
   */
  public void zip(boolean useSDcard) throws Exception {
    // prepare blacklist for no compression files
    HashSet<String> extension = new HashSet<String>();
    for (String s : noCompressExt) {
      extension.add(s);
    }

    fileList = FileUtils.getDirList(absolutePath);
    try {
      BufferedInputStream origin = null;
      FileOutputStream dest = null;
      if (useSDcard) {
        dest = new FileOutputStream(zipFile);
      } else {
        dest = activity.openFileOutput(zipFile, Context.MODE_WORLD_READABLE);
      }

      ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
      out.setLevel(9);

      byte data[] = new byte[BUFFER];

      CRC32 crc = new CRC32();

      for (String fileName : fileList) {
        // inputstream needs whole absolute path
        FileInputStream fi = new FileInputStream(absolutePath + fileName);
        origin = new BufferedInputStream(fi, BUFFER);
        // zip contains only path of target folder
        File file = new File(absolutePath + fileName);
        ZipEntry entry = new ZipEntry(fileName);
        if (isUncompressible(fileName, extension)) {

          entry.setMethod(ZipEntry.STORED);
          entry.setCompressedSize(file.length());
          entry.setSize(file.length());

          // crc32
          int bytesRead;
          byte[] buffer = new byte[1024];
          BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), 8192);
          crc.reset();
          while ((bytesRead = bis.read(buffer)) != -1) {
            crc.update(buffer, 0, bytesRead);
          }

          entry.setCrc(crc.getValue());
          out.putNextEntry(entry);
        } else {
          out.putNextEntry(entry);
        }
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
          out.write(data, 0, count);
        }
        origin.close();
      }

      out.close();
    } catch (Exception e) {
      Log.e(TAG, " Errror compressing a file " + e);
      throw new Exception();
    }
  }
Exemplo n.º 13
0
  private void writeEntry(
      JarOutputStream j,
      String name,
      long mtime,
      long lsize,
      boolean deflateHint,
      ByteBuffer data0,
      ByteBuffer data1)
      throws IOException {
    int size = (int) lsize;
    if (size != lsize) throw new IOException("file too large: " + lsize);

    CRC32 crc32 = _crc32;

    if (_verbose > 1)
      Utils.log.fine("Writing entry: " + name + " size=" + size + (deflateHint ? " deflated" : ""));

    if (_buf.length < size) {
      int newSize = size;
      while (newSize < _buf.length) {
        newSize <<= 1;
        if (newSize <= 0) {
          newSize = size;
          break;
        }
      }
      _buf = new byte[newSize];
    }
    assert (_buf.length >= size);

    int fillp = 0;
    if (data0 != null) {
      int size0 = data0.capacity();
      data0.get(_buf, fillp, size0);
      fillp += size0;
    }
    if (data1 != null) {
      int size1 = data1.capacity();
      data1.get(_buf, fillp, size1);
      fillp += size1;
    }
    while (fillp < size) {
      // Fill in rest of data from the stream itself.
      int nr = in.read(_buf, fillp, size - fillp);
      if (nr <= 0) throw new IOException("EOF at end of archive");
      fillp += nr;
    }

    ZipEntry z = new ZipEntry(name);
    z.setTime((long) mtime * 1000);

    if (size == 0) {
      z.setMethod(ZipOutputStream.STORED);
      z.setSize(0);
      z.setCrc(0);
      z.setCompressedSize(0);
    } else if (!deflateHint) {
      z.setMethod(ZipOutputStream.STORED);
      z.setSize(size);
      z.setCompressedSize(size);
      crc32.reset();
      crc32.update(_buf, 0, size);
      z.setCrc(crc32.getValue());
    } else {
      z.setMethod(Deflater.DEFLATED);
      z.setSize(size);
    }

    j.putNextEntry(z);

    if (size > 0) j.write(_buf, 0, size);

    j.closeEntry();
    if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z));
  }
  protected void processJarFile(final File file) throws Exception {

    if (verbose) {
      log("processing " + file.toURI());
    }

    final File tempFile =
        File.createTempFile(file.getName(), null, new File(file.getAbsoluteFile().getParent()));
    try {

      final ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
      try {
        final FileOutputStream fout = new FileOutputStream(tempFile);
        try {
          final ZipOutputStream out = new ZipOutputStream(fout);

          ZipEntry entry;
          while ((entry = zip.getNextEntry()) != null) {

            byte bytes[] = getBytes(zip);

            if (!entry.isDirectory()) {

              final DataInputStream din = new DataInputStream(new ByteArrayInputStream(bytes));

              if (din.readInt() == CLASS_MAGIC) {

                bytes = process(bytes);

              } else {
                if (verbose) {
                  log("ignoring " + entry.toString());
                }
              }
            }

            final ZipEntry outEntry = new ZipEntry(entry.getName());
            outEntry.setMethod(entry.getMethod());
            outEntry.setComment(entry.getComment());
            outEntry.setSize(bytes.length);

            if (outEntry.getMethod() == ZipEntry.STORED) {
              final CRC32 crc = new CRC32();
              crc.update(bytes);
              outEntry.setCrc(crc.getValue());
              outEntry.setCompressedSize(bytes.length);
            }
            out.putNextEntry(outEntry);
            out.write(bytes);
            out.closeEntry();
            zip.closeEntry();
          }
          out.close();
        } finally {
          fout.close();
        }
      } finally {
        zip.close();
      }

      if (file.delete()) {

        final File newFile = new File(tempFile.getAbsolutePath());

        if (!newFile.renameTo(file)) {
          throw new IOException("can not rename " + tempFile + " to " + file);
        }

      } else {
        throw new IOException("can not delete " + file);
      }

    } finally {

      tempFile.delete();
    }
  }