// Compresses files listed in "files" into a .zip file called "name"
 public void compress(ArrayList<String> files, String zipName) throws Exception {
   byte data[] = new byte[BUFFER];
   // File buffer streams
   BufferedInputStream origin = null;
   String zipDir = zipName;
   FileOutputStream zipDest = new FileOutputStream(zipDir);
   ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(zipDest));
   // Set compression method
   zipOut.setMethod(ZipOutputStream.DEFLATED);
   // Iterate compressing contained files
   for (int i = 0; i < files.size(); i++) {
     String address = files.get(i);
     FileInputStream fj = new FileInputStream(address);
     origin = new BufferedInputStream(fj, BUFFER);
     String[] splits = address.split("\\\\");
     String name = splits[splits.length - 1];
     ZipEntry entry = new ZipEntry(name);
     zipOut.putNextEntry(entry);
     int count;
     while ((count = origin.read(data, 0, BUFFER)) != -1) {
       zipOut.write(data, 0, count);
     }
     origin.close();
   }
   zipOut.close();
 }
Exemple #2
0
 /** Creates a new JAR file. */
 void create(OutputStream out, Manifest manifest) throws IOException {
   ZipOutputStream zos = new JarOutputStream(out);
   if (flag0) {
     zos.setMethod(ZipOutputStream.STORED);
   }
   if (manifest != null) {
     if (vflag) {
       output(getMsg("out.added.manifest"));
     }
     ZipEntry e = new ZipEntry(MANIFEST_DIR);
     e.setTime(System.currentTimeMillis());
     e.setSize(0);
     e.setCrc(0);
     zos.putNextEntry(e);
     e = new ZipEntry(MANIFEST_NAME);
     e.setTime(System.currentTimeMillis());
     if (flag0) {
       crc32Manifest(e, manifest);
     }
     zos.putNextEntry(e);
     manifest.write(zos);
     zos.closeEntry();
   }
   for (File file : entries) {
     addFile(zos, file);
   }
   zos.close();
 }
  public void installFramework(File frameFile, String tag) throws AndrolibException {
    InputStream in = null;
    ZipOutputStream out = null;
    try {
      ZipFile zip = new ZipFile(frameFile);
      ZipEntry entry = zip.getEntry("resources.arsc");

      if (entry == null) {
        throw new AndrolibException("Can't find resources.arsc file");
      }

      in = zip.getInputStream(entry);
      byte[] data = IOUtils.toByteArray(in);

      ARSCData arsc = ARSCDecoder.decode(new ByteArrayInputStream(data), true, true);
      publicizeResources(data, arsc.getFlagsOffsets());

      File outFile =
          new File(
              getFrameworkDir(),
              String.valueOf(arsc.getOnePackage().getId())
                  + (tag == null ? "" : '-' + tag)
                  + ".apk");

      out = new ZipOutputStream(new FileOutputStream(outFile));
      out.setMethod(ZipOutputStream.STORED);
      CRC32 crc = new CRC32();
      crc.update(data);
      entry = new ZipEntry("resources.arsc");
      entry.setSize(data.length);
      entry.setCrc(crc.getValue());
      out.putNextEntry(entry);
      out.write(data);

      LOGGER.info("Framework installed to: " + outFile);
    } catch (ZipException ex) {
      throw new AndrolibException(ex);
    } catch (IOException ex) {
      throw new AndrolibException(ex);
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException ex) {
        }
      }
      if (out != null) {
        try {
          out.close();
        } catch (IOException ex) {
        }
      }
    }
  }
  /** For testing */
  public void writeContext(
      IInteractionContext context, ZipOutputStream outputStream, IInteractionContextWriter writer)
      throws IOException {
    String handleIdentifier = context.getHandleIdentifier();
    String encoded =
        URLEncoder.encode(handleIdentifier, InteractionContextManager.CONTEXT_FILENAME_ENCODING);
    ZipEntry zipEntry =
        new ZipEntry(encoded + InteractionContextManager.CONTEXT_FILE_EXTENSION_OLD);
    outputStream.putNextEntry(zipEntry);
    outputStream.setMethod(ZipOutputStream.DEFLATED);

    writer.setOutputStream(outputStream);
    writer.writeContextToStream(context);
    outputStream.flush();
    outputStream.closeEntry();

    addAdditionalInformation(outputStream);
  }
  @Override
  public String call() throws Exception {
    // Needed Components
    DocumentDAO documentDAO = (DocumentDAO) Component.getInstance(DocumentDAO.class);
    LocaleDAO localeDAO = (LocaleDAO) Component.getInstance(LocaleDAO.class);
    ResourceUtils resourceUtils = (ResourceUtils) Component.getInstance(ResourceUtils.class);
    TextFlowTargetDAO textFlowTargetDAO =
        (TextFlowTargetDAO) Component.getInstance(TextFlowTargetDAO.class);
    FileSystemService fileSystemService =
        (FileSystemService) Component.getInstance(FileSystemServiceImpl.class);
    ConfigurationService configurationService =
        (ConfigurationService) Component.getInstance(ConfigurationServiceImpl.class);

    final String projectDirectory = projectSlug + "-" + iterationSlug + "/";
    final HLocale hLocale = localeDAO.findByLocaleId(new LocaleId(localeId));
    final String mappedLocale = hLocale.getLocaleId().getId();
    final String localeDirectory = projectDirectory + mappedLocale + "/";

    final File downloadFile = fileSystemService.createDownloadStagingFile("zip");
    final FileOutputStream output = new FileOutputStream(downloadFile);
    final ZipOutputStream zipOutput = new ZipOutputStream(output);
    zipOutput.setMethod(ZipOutputStream.DEFLATED);
    final PoWriter2 poWriter = new PoWriter2(false, !isPoProject);
    final Set<String> extensions = new HashSet<String>();

    extensions.add("gettext");
    extensions.add("comment");

    // Generate the download descriptor file
    String downloadId =
        fileSystemService.createDownloadDescriptorFile(
            downloadFile, projectSlug + "_" + iterationSlug + "_" + localeId + ".zip", userName);

    // Add the config file at the root of the project directory
    String configFilename = projectDirectory + configurationService.getConfigurationFileName();
    zipOutput.putNextEntry(new ZipEntry(configFilename));
    zipOutput.write(
        configurationService
            .getConfigForOfflineTranslation(projectSlug, iterationSlug, hLocale)
            .getBytes());
    zipOutput.closeEntry();
    getHandle().increaseProgress(1);

    final List<HDocument> allIterationDocs =
        documentDAO.getAllByProjectIteration(projectSlug, iterationSlug);
    for (HDocument document : allIterationDocs) {
      // Stop the process if signaled to do so
      if (getHandle().isCancelled()) {
        zipOutput.close();
        downloadFile.delete();
        fileSystemService.deleteDownloadDescriptorFile(downloadId);
        return null;
      }

      TranslationsResource translationResource = new TranslationsResource();
      List<HTextFlowTarget> hTargets = textFlowTargetDAO.findTranslations(document, hLocale);
      resourceUtils.transferToTranslationsResource(
          translationResource, document, hLocale, extensions, hTargets, Optional.<String>absent());

      Resource res = resourceUtils.buildResource(document);

      String filename = localeDirectory + document.getDocId() + ".po";
      zipOutput.putNextEntry(new ZipEntry(filename));
      poWriter.writePo(zipOutput, "UTF-8", res, translationResource);
      zipOutput.closeEntry();

      getHandle().increaseProgress(1);
    }

    zipOutput.flush();
    zipOutput.close();

    return downloadId;
  }