/**
  * lzma 解壓縮
  *
  * @param value
  * @return
  */
 public static byte[] unlzma(byte[] value) {
   byte[] result = new byte[0];
   //
   ByteArrayInputStream in = null;
   ByteArrayOutputStream out = null;
   try {
     Decoder decoder = new Decoder();
     byte[] properties = new byte[5];
     System.arraycopy(value, 0, properties, 0, 5);
     if (properties.length != 5) {
       throw new RuntimeException("input .lzma is too short");
     }
     decoder.SetDecoderProperties(properties);
     //
     int length = value.length - properties.length;
     byte[] data = new byte[length];
     System.arraycopy(value, properties.length, data, 0, length);
     //
     in = new ByteArrayInputStream(data);
     out = new ByteArrayOutputStream();
     decoder.Code(in, out, -1);
     result = out.toByteArray();
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     IoHelper.close(in);
     IoHelper.close(out);
   }
   return result;
 }
Exemple #2
0
  public void run() {
    try {
      for (Slice.FileData data = slice.readNextFile(); data != null; data = slice.readNextFile()) {
        FileList.FileLocation file = data.file;

        ByteArrayInputStream in = new ByteArrayInputStream(data.data);
        decoder.SetDecoderProperties(data.props);

        String fileName = destpath + "/" + file.fileName;
        File f = new File(fileName);
        f.getParentFile().mkdirs();

        // check if we need to undo the tranformation on x86 executables
        if ((file.fileName.contains(".exe")
            || file.fileName.contains(".dll"))) { // TODO there is an attribute for this
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          decoder.Code(in, out, file.originalSize);
          byte[] decompressedData = out.toByteArray();
          Util.transformCallInstructions(decompressedData);
          BufferedOutputStream outfile = new BufferedOutputStream(new FileOutputStream(f));
          outfile.write(decompressedData);
          outfile.close();
        } else {
          BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
          decoder.Code(in, out, file.originalSize);
          out.close();
        }
        f.setLastModified(file.mtime.getTime());
        ui.increaseProgress();
      }
    } catch (Exception e) {
      ui.showError(e.getLocalizedMessage());
      e.printStackTrace();
    }
  }
  private static void extractLzmaToFile(InputStream srcStream, File destFile) throws IOException {
    InputStream input = null;
    OutputStream output = null;

    try {
      input = new BufferedInputStream(srcStream);
      output = new BufferedOutputStream(new FileOutputStream(destFile));

      byte[] properties = new byte[LZMA_PROP_SIZE];
      if (input.read(properties, 0, LZMA_PROP_SIZE) != LZMA_PROP_SIZE) {
        throw new IOException("Input lzma file is too short");
      }

      Decoder decoder = new Decoder();
      if (!decoder.SetDecoderProperties(properties)) {
        throw new IOException("Incorrect lzma properties");
      }

      long outSize = 0;
      for (int i = 0; i < LZMA_OUTSIZE; i++) {
        int v = input.read();
        if (v < 0) {
          Log.w(TAG, "Can't read stream size");
        }
        outSize |= ((long) v) << (8 * i);
      }

      if (!decoder.Code(input, output, outSize)) {
        throw new IOException("Error in data stream");
      }
    } catch (IOException e) {
      if (destFile.isFile()) destFile.delete();
      throw e;
    } finally {
      try {
        output.flush();
      } catch (IOException | NullPointerException e) {
      }
      try {
        output.close();
      } catch (IOException | NullPointerException e) {
      }
      try {
        input.close();
      } catch (IOException | NullPointerException e) {
      }
    }
  }