Пример #1
0
  private static void addFilesToCompression(ZipArchiveOutputStream zaos, File file, String dir)
      throws IOException {

    ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, dir + file.getName());
    zaos.putArchiveEntry(zipArchiveEntry);

    if (file.isFile()) {
      BufferedInputStream bis = null;
      try {
        bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, zaos);
        zaos.closeArchiveEntry();
      } catch (IOException e) {
        throw e;
      } finally {
        IOUtils.closeQuietly(bis);
      }
    } else if (file.isDirectory()) {
      zaos.closeArchiveEntry();

      for (File childFile : file.listFiles()) {
        addFilesToCompression(zaos, childFile, dir + file.getName() + File.separator);
      }
    }
  }
Пример #2
0
  private void doInternalArchive(
      File source, ZipArchiveOutputStream zos, String entryName, List<Throwable> throwables)
      throws IOException {
    if (source.isDirectory() && !source.isHidden()) {

      zos.putArchiveEntry(new ZipArchiveEntry(entryName + separator));

      for (File child : source.listFiles()) {
        doInternalArchive(child, zos, entryName + separator + child.getName(), throwables);
      }

    } else {

      if (source.isHidden()) {
        return;
      }

      InputStream in = null;

      try {

        ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
        zos.putArchiveEntry(entry);

        in = new FileInputStream(source);
        IOs.piping(in, zos);
        zos.closeArchiveEntry();

      } catch (Exception e) {
        throwables.add(e);
      } finally {
        IOs.freeQuietly(in);
      }
    }
  }
Пример #3
0
 @Test
 public void testZipAndUnzipDirectory() throws IOException {
   File basedir = File.createTempFile(ZipUtils.class.getSimpleName(), ".tmp");
   basedir.delete();
   assertTrue("Could not create temporary base directory.", basedir.mkdir());
   for (int i = 0; i < 5; i++) {
     createSubDirWithFiles(basedir, i);
   }
   File zipFile = File.createTempFile(ZipUtils.class.getSimpleName(), ".zip");
   ZipArchiveOutputStream zipOutputStream =
       new ZipArchiveOutputStream(new FileOutputStream(zipFile));
   ZipUtils.addFilesToZipRecursively("mybase", basedir, null, zipOutputStream);
   zipOutputStream.close();
   final File unzipBase = File.createTempFile(ZipUtils.class.getSimpleName(), ".unzipped");
   ZipUtils.unzip(zipFile, unzipBase);
   IOUtils.processFiles(
       unzipBase,
       new FileProcessor() {
         public void process(File file) {
           try {
             assertEquals(
                 IOUtils.getFileIdentifier(unzipBase, file), FileUtils.readFileToString(file));
           } catch (IOException e) {
             throw new RuntimeException("Could not process file.", e);
           }
         }
       },
       null);
 }
Пример #4
0
  public static final void zip(String compressPath, String[] needCompressPaths) {
    File compressFile = new File(compressPath);

    List<File> files = Lists.newArrayList();
    for (String needCompressPath : needCompressPaths) {
      File needCompressFile = new File(needCompressPath);
      if (!needCompressFile.exists()) {
        continue;
      }
      files.add(needCompressFile);
    }
    try {
      ZipArchiveOutputStream zaos = null;
      try {
        zaos = new ZipArchiveOutputStream(compressFile);
        zaos.setUseZip64(Zip64Mode.AsNeeded);
        zaos.setEncoding("GBK");

        for (File file : files) {
          addFilesToCompression(zaos, file, "");
        }

      } catch (IOException e) {
        throw e;
      } finally {
        IOUtils.closeQuietly(zaos);
      }
    } catch (Exception e) {
      FileUtils.deleteQuietly(compressFile);
      throw new RuntimeException("压缩失败", e);
    }
  }
Пример #5
0
  protected boolean createEmptyZip(File zipFile) throws ArchiverException {
    if (!createEmpty) {
      return true;
    }

    ZipArchiveOutputStream zOut = null;
    try {
      getLogger().debug("Building MANIFEST-only jar: " + getDestFile().getAbsolutePath());
      zOut =
          new ZipArchiveOutputStream(bufferedOutputStream(fileOutputStream(getDestFile(), "jar")));

      zOut.setEncoding(getEncoding());
      if (isCompress()) {
        zOut.setMethod(ZipArchiveOutputStream.DEFLATED);
      } else {
        zOut.setMethod(ZipArchiveOutputStream.STORED);
      }
      initZipOutputStream(zOut);
      finalizeZipOutputStream(zOut);
    } catch (IOException ioe) {
      throw new ArchiverException(
          "Could not create almost empty JAR archive (" + ioe.getMessage() + ")", ioe);
    } finally {
      // Close the output stream.
      IOUtil.close(zOut);
      createEmpty = false;
    }
    return true;
  }
  @Test
  public void testDeserializeZipArchiveSimple() throws Exception {
    // produce a zip archive containing a single serialized stream for this test.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(baos);
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(StreamId.APPLICATION_VERSION.name());
    zipOut.putArchiveEntry(zipEntry);
    IOUtils.copy(APPLICATION_VERSION_1.getInputStream(), zipOut);
    zipOut.closeArchiveEntry();
    zipOut.close();

    state = new PackageState();
    when(mockedMarshallerMap
            .get(StreamId.APPLICATION_VERSION)
            .getUnmarshaller()
            .unmarshal(any(Source.class)))
        .thenReturn(applicationVersion);
    ByteArrayInputStream zipIn = new ByteArrayInputStream(baos.toByteArray());
    underTest.deserialize(state, StreamId.APPLICATION_VERSION, zipIn);

    assertNotNull(state.getCreationToolVersion());
    assertEquals(applicationVersion, state.getCreationToolVersion());
    verify(mockedMarshallerMap.get(StreamId.APPLICATION_VERSION).getUnmarshaller())
        .unmarshal(any(Source.class));
  }
Пример #7
0
  public void compress(String contentsDirPath) throws IOException {
    List<File> files = new ArrayList<File>();
    File contentsDir = new File(contentsDirPath);
    listFileAndDirectories(contentsDir, files);
    InputStream in = null;
    ZipArchiveOutputStream out = null;

    try {
      out = new ZipArchiveOutputStream(new File(fileName));
      String contentsDirAbsolutePath = contentsDir.getAbsolutePath();
      for (File file : files) {
        String entryName =
            file.getAbsolutePath().substring(contentsDirAbsolutePath.length() + 1)
                + (file.isDirectory() ? File.separator : "");
        ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
        out.putArchiveEntry(entry);
        if (file.isDirectory()) {
          continue;
        }
        in = new FileInputStream(file);
        copy(in, out);
        out.closeArchiveEntry();
        closeQuietly(in);
      }
    } finally {
      closeQuietly(in);
      closeQuietly(out);
    }
  }
Пример #8
0
  public static void compress(
      File[] srcFiles, File zipFile, String compressPath, String encoding, String comment) {

    ZipArchiveOutputStream zaos = null;
    try {
      zaos = new ZipArchiveOutputStream(zipFile);

      CompressUtilsHelper.config(zaos, encoding, comment);

      if (ValidateUtilsHelper.isBlank(compressPath)) {
        compressPath = CommonsConstants.EMPTY;
      }

      for (File file : srcFiles) {
        String zipFilePath = FilenameUtils.concat(compressPath, file.getName());
        CompressUtilsHelper.compress(file, zaos, zipFilePath);
      }

      zaos.closeArchiveEntry();
      zaos.finish();
    } catch (Exception e) {
      throw new CommonsException("Compress files to " + zipFile.getPath() + " failed!", e);
    } finally {
      IOUtils.closeQuietly(zaos);
    }
  }
Пример #9
0
    public void packageToFile(File packageFile) throws Exception {
      F2<
              RelativePath,
              PackageFileSystemObject<F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>>,
              Boolean>
          pathFilter = pathFilter();

      fileSystem = fileSystem.prettify();

      Stream<PackageFileSystemObject<F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>>> items =
          fileSystem.toList().filter(compose(BooleanF.invert, curry(pathFilter, BASE)));

      ZipArchiveOutputStream zos = null;
      try {
        zos = new ZipArchiveOutputStream(packageFile);
        zos.setLevel(Deflater.BEST_COMPRESSION);

        for (PackageFileSystemObject<F2<UnixFsObject, ZipArchiveOutputStream, IoEffect>>
            fileSystemObject : items) {
          fileSystemObject.getExtension().f(fileSystemObject.getUnixFsObject(), zos).run();
        }
      } finally {
        IOUtil.close(zos);
      }
    }
Пример #10
0
 /**
  * 设置压缩文件输出流
  *
  * @param zaos 压缩文件输出流
  * @param encoding 字符集
  * @param comment 注释
  */
 private static void config(ZipArchiveOutputStream zaos, String encoding, String comment) {
   zaos.setUseZip64(Zip64Mode.AsNeeded);
   if (ValidateUtilsHelper.isNotBlank(encoding)) {
     zaos.setEncoding(encoding);
   }
   if (ValidateUtilsHelper.isNotBlank(comment)) {
     zaos.setComment(comment);
   }
 }
Пример #11
0
 /**
  * Write an XML serialized XWikiDocument to a ZipOutputStream
  *
  * @param doc the document to serialize
  * @param zos the ZipOutputStream to write to
  * @param withVersions if true, also serialize all document versions
  * @param context current XWikiContext
  * @throws XWikiException when an error occurs during documents access
  * @throws IOException when an error occurs during streaming operation
  * @since 4.1M2
  */
 private void addToZip(
     XWikiDocument doc, ZipArchiveOutputStream zos, boolean withVersions, XWikiContext context)
     throws XWikiException, IOException {
   String zipname = getPathFromDocument(doc, context);
   ZipArchiveEntry zipentry = new ZipArchiveEntry(zipname);
   zos.putArchiveEntry(zipentry);
   doc.toXML(zos, true, false, true, withVersions, context);
   zos.closeArchiveEntry();
 }
Пример #12
0
 /**
  * Write the package.xml file to a ZipOutputStream
  *
  * @param zos the ZipOutputStream to write to
  * @param context current XWikiContext
  */
 private void addInfosToZip(ZipArchiveOutputStream zos, XWikiContext context) {
   try {
     String zipname = DefaultPackageFileName;
     ZipArchiveEntry zipentry = new ZipArchiveEntry(zipname);
     zos.putArchiveEntry(zipentry);
     toXML(zos, context);
     zos.closeArchiveEntry();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Пример #13
0
 /**
  * 压缩文件或空目录
  *
  * @param srcFile 文件或空目录
  * @param zaos 压缩输出流
  * @param compressPath 压缩路径
  */
 private static void addZipEntry(File srcFile, ZipArchiveOutputStream zaos, String compressPath) {
   try {
     ZipArchiveEntry zipEntry = new ZipArchiveEntry(srcFile, compressPath);
     zaos.putArchiveEntry(zipEntry);
     if (srcFile.isFile()) {
       FileUtilsHelper.copyFile(srcFile, zaos);
     }
   } catch (Exception e) {
     throw new CommonsException(
         "Compress " + srcFile.getPath() + " to " + compressPath + " failed!", e);
   }
 }
Пример #14
0
  public String export(OutputStream os, XWikiContext context) throws IOException, XWikiException {
    if (this.files.size() == 0) {
      return "No Selected file";
    }

    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
    zos.setEncoding(XAR_FILENAME_ENCODING);
    // By including the unicode extra fields, it is possible to extract XAR-files
    // containing documents with non-ascii characters in the document name using InfoZIP,
    // and the filenames will be correctly converted to the character set of the local
    // file system.
    zos.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
    for (int i = 0; i < this.files.size(); i++) {
      DocumentInfo docinfo = this.files.get(i);
      XWikiDocument doc = docinfo.getDoc();
      addToZip(doc, zos, this.withVersions, context);
    }
    addInfosToZip(zos, context);
    zos.finish();
    zos.flush();

    return "";
  }
Пример #15
0
  public void execute() {

    List<Throwable> throwables = Lists.newLinkedList();
    ZipArchiveOutputStream zos = null;

    try {

      zos =
          (ZipArchiveOutputStream)
              new ArchiveStreamFactory()
                  .createArchiveOutputStream("zip", new FileOutputStream(target));
      zos.setEncoding(encoding);
      doInternalArchive(source, zos, "", throwables);

    } catch (Exception e) {
      throwables.add(e);
    } finally {
      IOs.freeQuietly(zos);
    }

    if (!throwables.isEmpty()) {
      throw Lang.comboThrow(throwables);
    }
  }
Пример #16
0
  public static void compress(
      File srcFile, File zipFile, String compressPath, String encoding, String comment) {

    ZipArchiveOutputStream zaos = null;
    try {
      zaos = new ZipArchiveOutputStream(zipFile);

      CompressUtilsHelper.config(zaos, encoding, comment);

      if (ValidateUtilsHelper.isBlank(compressPath)) {
        compressPath = srcFile.getName();
      }

      CompressUtilsHelper.compress(srcFile, zaos, compressPath);

      zaos.closeArchiveEntry();
      zaos.finish();
    } catch (Exception e) {
      throw new CommonsException(
          "Compress " + srcFile.getPath() + " to " + zipFile.getPath() + " failed!", e);
    } finally {
      IOUtils.closeQuietly(zaos);
    }
  }
Пример #17
0
  private void start() throws Exception {
    super.dbInit();
    SystemConfigsEntity entity =
        SystemConfigsDao.get()
            .selectOnKey(SystemConfig.DATA_EXPORT, AppConfig.get().getSystemName());
    if (entity == null) {
      send("[Fail] create fail. please try again.");
      return;
    }
    // エクスポートデータを格納するディレクトリ
    AppConfig config = AppConfig.get();
    File base = new File(config.getTmpPath());
    File dir = new File(base, DATA_DIR);
    if (dir.exists()) {
      FileUtil.delete(dir);
    }
    dir.mkdirs();
    File attach = new File(dir, "attach");
    attach.mkdirs();
    File userdir = new File(dir, "user");
    userdir.mkdirs();

    // ナレッジデータを取得
    LoginedUser loginedUser =
        new LoginedUser() {
          @Override
          public boolean isAdmin() {
            return true;
          }
        };
    KnowledgeLogic knowledgeLogic = KnowledgeLogic.get();
    CommentsDao commentsDao = CommentsDao.get();
    KnowledgeFilesDao knowledgeFilesDao = KnowledgeFilesDao.get();

    List<KnowledgesEntity> knowledges = knowledgeLogic.searchKnowledge("", loginedUser, 0, 100);
    for (KnowledgesEntity knowledgesEntity : knowledges) {
      File f =
          new File(
              dir,
              "knowledge-"
                  + StringUtils.zeroPadding(knowledgesEntity.getKnowledgeId(), 6)
                  + ".xml");
      String xml = SerializeUtils.objectToXml(knowledgesEntity);
      FileUtil.write(f, xml);

      // ナレッジのコメントを取得
      List<CommentsEntity> comments =
          commentsDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId());
      for (CommentsEntity commentsEntity : comments) {
        File c =
            new File(
                dir,
                "comment-"
                    + StringUtils.zeroPadding(knowledgesEntity.getKnowledgeId(), 6)
                    + "-"
                    + StringUtils.zeroPadding(commentsEntity.getKnowledgeId(), 6)
                    + ".xml");
        xml = SerializeUtils.objectToXml(commentsEntity);
        FileUtil.write(c, xml);
      }
      List<KnowledgeFilesEntity> files =
          knowledgeFilesDao.selectOnKnowledgeId(knowledgesEntity.getKnowledgeId());
      for (KnowledgeFilesEntity knowledgeFilesEntity : files) {
        KnowledgeFilesEntity file = knowledgeFilesDao.selectOnKey(knowledgeFilesEntity.getFileNo());
        File a =
            new File(
                attach,
                "attach-"
                    + StringUtils.zeroPadding(knowledgesEntity.getKnowledgeId(), 6)
                    + "-"
                    + StringUtils.zeroPadding(knowledgeFilesEntity.getFileNo(), 6)
                    + knowledgeFilesEntity.getFileName());
        OutputStream outputStream = new FileOutputStream(a);
        InputStream inputStream = file.getFileBinary();
        try {
          FileUtil.copy(inputStream, outputStream);
        } finally {
          inputStream.close();
          outputStream.close();
        }
      }
      send("[Export] knowledge-" + knowledgesEntity.getKnowledgeId());
    }

    // ユーザ情報を取得
    UsersDao usersDao = UsersDao.get();
    List<UsersEntity> users = usersDao.selectAll();
    for (UsersEntity usersEntity : users) {
      ExportUser user = new ExportUser();
      PropertyUtil.copyPropertyValue(usersEntity, user);
      File f = new File(userdir, "user-" + StringUtils.zeroPadding(user.getUserId(), 6) + ".xml");
      String xml = SerializeUtils.objectToXml(user);
      FileUtil.write(f, xml);
      send("[Export] user-" + user.getUserId());
    }

    // zip圧縮
    send("[Export] during the compression");
    String name = DATA_DIR + ".zip";
    File comp = new File(base, name);

    BufferedOutputStream output = null;
    ZipArchiveOutputStream os = null;
    try {
      output = new BufferedOutputStream(new FileOutputStream(comp));
      os = new ZipArchiveOutputStream(output);
      os.setEncoding("UTF-8");
      CompressLogic.get().addZip(os, dir, null);
    } finally {
      if (os != null) {
        os.close();
      }
      if (output != null) {
        output.close();
      }
    }
    send("[Export] complete!");
    // 圧縮の予約を削除
    SystemConfigsDao.get().physicalDelete(entity);
  }
Пример #18
0
  private static byte[] getX509Zip(User u) throws Exception {
    X509Certificate cloudCert = null;
    final X509Certificate x509;
    String userAccessKey = null;
    String userSecretKey = null;
    KeyPair keyPair = null;
    try {
      for (AccessKey k : u.getKeys()) {
        if (k.isActive()) {
          userAccessKey = k.getAccessKey();
          userSecretKey = k.getSecretKey();
        }
      }
      if (userAccessKey == null) {
        AccessKey k = u.createKey();
        userAccessKey = k.getAccessKey();
        userSecretKey = k.getSecretKey();
      }
      keyPair = Certs.generateKeyPair();
      x509 = Certs.generateCertificate(keyPair, u.getName());
      x509.checkValidity();
      u.addCertificate(x509);
      cloudCert = SystemCredentials.lookup(Eucalyptus.class).getCertificate();
    } catch (Exception e) {
      LOG.fatal(e, e);
      throw e;
    }
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(byteOut);
    ZipArchiveEntry entry = null;
    String fingerPrint = Certs.getFingerPrint(keyPair.getPublic());
    if (fingerPrint != null) {
      String baseName =
          X509Download.NAME_SHORT
              + "-"
              + u.getName()
              + "-"
              + fingerPrint.replaceAll(":", "").toLowerCase().substring(0, 8);

      zipOut.setComment("To setup the environment run: source /path/to/eucarc");
      StringBuilder sb = new StringBuilder();
      // TODO:GRZE:FIXME velocity
      String userNumber = u.getAccount().getAccountNumber();
      sb.append("EUCA_KEY_DIR=$(dirname $(readlink -f ${BASH_SOURCE}))");
      if (Topology.isEnabled(Eucalyptus.class)) { // GRZE:NOTE: this is temporary
        sb.append(
            "\nexport EC2_URL=" + ServiceUris.remotePublicify(Topology.lookup(Eucalyptus.class)));
      } else {
        sb.append("\necho WARN:  Eucalyptus URL is not configured. >&2");
        ServiceBuilder<? extends ServiceConfiguration> builder =
            ServiceBuilders.lookup(Eucalyptus.class);
        ServiceConfiguration localConfig =
            builder.newInstance(
                Internets.localHostAddress(),
                Internets.localHostAddress(),
                Internets.localHostAddress(),
                Eucalyptus.INSTANCE.getPort());
        sb.append("\nexport EC2_URL=" + ServiceUris.remotePublicify(localConfig));
      }
      if (Topology.isEnabled(Walrus.class)) {
        ServiceConfiguration walrusConfig = Topology.lookup(Walrus.class);
        try {
          String uri = ServiceUris.remotePublicify(walrusConfig).toASCIIString();
          LOG.debug("Found walrus uri/configuration: uri=" + uri + " config=" + walrusConfig);
          sb.append("\nexport S3_URL=" + uri);
        } catch (Exception e) {
          LOG.error("Failed to set Walrus URL: " + walrusConfig, e);
        }
      } else {
        sb.append("\necho WARN:  Walrus URL is not configured. >&2");
      }
      // Disable notifications for now
      // sb.append( "\nexport AWS_SNS_URL=" + ServiceUris.remote( Notifications.class ) );
      if (Topology.isEnabled(Euare.class)) { // GRZE:NOTE: this is temporary
        sb.append("\nexport EUARE_URL=" + ServiceUris.remotePublicify(Euare.class));
      } else {
        sb.append("\necho WARN:  EUARE URL is not configured. >&2");
      }
      sb.append("\nexport EC2_PRIVATE_KEY=${EUCA_KEY_DIR}/" + baseName + "-pk.pem");
      sb.append("\nexport EC2_CERT=${EUCA_KEY_DIR}/" + baseName + "-cert.pem");
      sb.append("\nexport EC2_JVM_ARGS=-Djavax.net.ssl.trustStore=${EUCA_KEY_DIR}/jssecacerts");
      sb.append("\nexport EUCALYPTUS_CERT=${EUCA_KEY_DIR}/cloud-cert.pem");
      sb.append("\nexport EC2_ACCOUNT_NUMBER='" + u.getAccount().getAccountNumber() + "'");
      sb.append("\nexport EC2_ACCESS_KEY='" + userAccessKey + "'");
      sb.append("\nexport EC2_SECRET_KEY='" + userSecretKey + "'");
      sb.append("\nexport AWS_CREDENTIAL_FILE=${EUCA_KEY_DIR}/iamrc");
      sb.append("\nexport EC2_USER_ID='" + userNumber + "'");
      sb.append(
          "\nalias ec2-bundle-image=\"ec2-bundle-image --cert ${EC2_CERT} --privatekey ${EC2_PRIVATE_KEY} --user ${EC2_ACCOUNT_NUMBER} --ec2cert ${EUCALYPTUS_CERT}\"");
      sb.append(
          "\nalias ec2-upload-bundle=\"ec2-upload-bundle -a ${EC2_ACCESS_KEY} -s ${EC2_SECRET_KEY} --url ${S3_URL}\"");
      sb.append("\n");
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry("eucarc"));
      entry.setUnixMode(0600);
      zipOut.write(sb.toString().getBytes("UTF-8"));
      zipOut.closeArchiveEntry();

      sb = new StringBuilder();
      sb.append("AWSAccessKeyId=").append(userAccessKey).append('\n');
      sb.append("AWSSecretKey=").append(userSecretKey);
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry("iamrc"));
      entry.setUnixMode(0600);
      zipOut.write(sb.toString().getBytes("UTF-8"));
      zipOut.closeArchiveEntry();

      /** write the private key to the zip stream * */
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry("cloud-cert.pem"));
      entry.setUnixMode(0600);
      zipOut.write(PEMFiles.getBytes(cloudCert));
      zipOut.closeArchiveEntry();

      zipOut.putArchiveEntry(entry = new ZipArchiveEntry("jssecacerts"));
      entry.setUnixMode(0600);
      KeyStore tempKs = KeyStore.getInstance("jks");
      tempKs.load(null);
      tempKs.setCertificateEntry("eucalyptus", cloudCert);
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      tempKs.store(bos, "changeit".toCharArray());
      zipOut.write(bos.toByteArray());
      zipOut.closeArchiveEntry();

      /** write the private key to the zip stream * */
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry(baseName + "-pk.pem"));
      entry.setUnixMode(0600);
      zipOut.write(PEMFiles.getBytes(keyPair.getPrivate()));
      zipOut.closeArchiveEntry();

      /** write the X509 certificate to the zip stream * */
      zipOut.putArchiveEntry(entry = new ZipArchiveEntry(baseName + "-cert.pem"));
      entry.setUnixMode(0600);
      zipOut.write(PEMFiles.getBytes(x509));
      zipOut.closeArchiveEntry();
    }
    /** close the zip output stream and return the bytes * */
    zipOut.close();
    return byteOut.toByteArray();
  }
  /* package */ File createAndAddCustomizedAndroidManifestToSelendroidServer()
      throws IOException, ShellCommandException, AndroidSdkException {
    String targetPackageName = applicationUnderTest.getBasePackage();
    File tempdir =
        new File(
            FileUtils.getTempDirectoryPath()
                + File.separatorChar
                + targetPackageName
                + System.currentTimeMillis());

    if (!tempdir.exists()) {
      tempdir.mkdirs();
    }

    File customizedManifest = new File(tempdir, "AndroidManifest.xml");
    log.info(
        "Adding target package '"
            + targetPackageName
            + "' to "
            + customizedManifest.getAbsolutePath());

    // add target package
    InputStream inputStream = getResourceAsStream(selendroidApplicationXmlTemplate);
    if (inputStream == null) {
      throw new SelendroidException("AndroidApplication.xml template file was not found.");
    }
    String content = IOUtils.toString(inputStream, Charset.defaultCharset().displayName());

    // find the first occurance of "package" and appending the targetpackagename to begining
    int i = content.toLowerCase().indexOf("package");
    int cnt = 0;
    for (; i < content.length(); i++) {
      if (content.charAt(i) == '\"') {
        cnt++;
      }
      if (cnt == 2) {
        break;
      }
    }
    content = content.substring(0, i) + "." + targetPackageName + content.substring(i);
    log.info("Final Manifest File:\n" + content);
    content = content.replaceAll(SELENDROID_TEST_APP_PACKAGE, targetPackageName);
    // Seems like this needs to be done
    if (content.contains(ICON)) {
      content = content.replaceAll(ICON, "");
    }

    OutputStream outputStream = new FileOutputStream(customizedManifest);
    IOUtils.write(content, outputStream, Charset.defaultCharset().displayName());
    IOUtils.closeQuietly(inputStream);
    IOUtils.closeQuietly(outputStream);

    // adding the xml to an empty apk
    CommandLine createManifestApk = new CommandLine(AndroidSdk.aapt());

    createManifestApk.addArgument("package", false);
    createManifestApk.addArgument("-M", false);
    createManifestApk.addArgument(customizedManifest.getAbsolutePath(), false);
    createManifestApk.addArgument("-I", false);
    createManifestApk.addArgument(AndroidSdk.androidJar(), false);
    createManifestApk.addArgument("-F", false);
    createManifestApk.addArgument(
        tempdir.getAbsolutePath() + File.separatorChar + "manifest.apk", false);
    createManifestApk.addArgument("-f", false);
    log.info(ShellCommand.exec(createManifestApk, 20000L));

    ZipFile manifestApk =
        new ZipFile(new File(tempdir.getAbsolutePath() + File.separatorChar + "manifest.apk"));
    ZipArchiveEntry binaryManifestXml = manifestApk.getEntry("AndroidManifest.xml");

    File finalSelendroidServerFile = new File(tempdir.getAbsolutePath() + "selendroid-server.apk");
    ZipArchiveOutputStream finalSelendroidServer =
        new ZipArchiveOutputStream(finalSelendroidServerFile);
    finalSelendroidServer.putArchiveEntry(binaryManifestXml);
    IOUtils.copy(manifestApk.getInputStream(binaryManifestXml), finalSelendroidServer);

    ZipFile selendroidPrebuildApk = new ZipFile(selendroidServer.getAbsolutePath());
    Enumeration<ZipArchiveEntry> entries = selendroidPrebuildApk.getEntries();
    for (; entries.hasMoreElements(); ) {
      ZipArchiveEntry dd = entries.nextElement();
      finalSelendroidServer.putArchiveEntry(dd);

      IOUtils.copy(selendroidPrebuildApk.getInputStream(dd), finalSelendroidServer);
    }

    finalSelendroidServer.closeArchiveEntry();
    finalSelendroidServer.close();
    manifestApk.close();
    log.info("file: " + finalSelendroidServerFile.getAbsolutePath());
    return finalSelendroidServerFile;
  }