/**
   * Unzip all the projects contained by the zip.
   *
   * @param bundleName the bundle name
   * @param zipLocation the zip location inside of the bundle
   * @param monitor the progress monitor
   * @throws IOException if there is an issue copying a file from the zip
   * @throws CoreException if there is an issue creating one of the projects
   */
  public static void unzipAllProjects(
      String bundleName, String zipLocation, IProgressMonitor monitor)
      throws IOException, CoreException {
    final URL interpreterZipUrl =
        FileLocator.find(Platform.getBundle(bundleName), new Path(zipLocation), null);
    final ZipInputStream zipFileStream = new ZipInputStream(interpreterZipUrl.openStream());
    ZipEntry zipEntry = zipFileStream.getNextEntry();

    Set<IProject> projects = new HashSet<IProject>();

    while (zipEntry != null) {
      String projectName = zipEntry.getName().split("/")[0];

      IProject project = createProject(projectName, monitor);
      projects.add(project);

      final File file =
          new File(
              project.getLocation().toString(),
              zipEntry
                  .getName()
                  .replaceFirst(projectName + "/", "")); // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$

      if (!zipEntry.isDirectory()) {

        /*
         * Copy files (and make sure parent directory exist)
         */
        final File parentFile = file.getParentFile();
        if (null != parentFile && !parentFile.exists()) {
          parentFile.mkdirs();
        }

        OutputStream os = null;

        try {
          os = new FileOutputStream(file);

          final int bufferSize = 102400;
          final byte[] buffer = new byte[bufferSize];
          while (true) {
            final int len = zipFileStream.read(buffer);
            if (zipFileStream.available() == 0) {
              break;
            }
            os.write(buffer, 0, len);
          }
        } finally {
          if (null != os) {
            os.close();
          }
        }
      }
      zipFileStream.closeEntry();
      zipEntry = zipFileStream.getNextEntry();
    }

    ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, monitor);
  }
Пример #2
0
  /**
   * Reads data for the <em>current</em> entry from the zip input stream.
   *
   * @param zis The Zip input stream
   * @param entrySize The entry size. -1 if unknown.
   * @return The new data for the <em>current</em> entry.
   * @throws IOException If ZipInputStream.read() fails.
   */
  private byte[] readZipData(ZipInputStream zis, int entrySize) throws IOException {
    int block_size = 1024;
    int data_size = entrySize < 1 ? block_size : entrySize;
    int offset = 0;
    byte[] data = new byte[data_size];

    while (zis.available() != 0) {
      int count = zis.read(data, offset, data_size - offset);
      if (count < 0) { // read data is done
        break;
      }
      offset += count;

      if (entrySize >= 1 && offset >= entrySize) { // we know the size and we're done
        break;
      }

      // if we don't know the entry size and we're not done reading,
      // expand the data buffer some more.
      if (offset >= data_size) {
        byte[] temp = new byte[data_size + block_size];
        System.arraycopy(data, 0, temp, 0, data_size);
        data_size += block_size;
        data = temp;
        block_size *= 2;
      }
    }

    if (offset < data_size) {
      // buffer was allocated too large, trim it
      byte[] temp = new byte[offset];
      if (offset > 0) {
        System.arraycopy(data, 0, temp, 0, offset);
      }
      data = temp;
    }

    return data;
  }
  /** {@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);
        }
      }
    }
  }
Пример #4
0
 @Override
 public int available() throws IOException {
   return zipInputStream.available();
 }