com.gabrielittner.github.diff.androidmanifest.AndroidXmlTranslator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of github-diff Show documentation
Show all versions of github-diff Show documentation
Diff apks and post results to Github
/*
* Copyright 2015 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gabrielittner.github.diff.androidmanifest;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Is a function : (apk file) --> ,manifest XML text, as list of tokens with tag
* based om code posted on StackOverflow by Ribo:
* http://stackoverflow.com/a/4761689/496992
*
* There is a bug that some manifests can't be shown, added a fallback case to display
* all strings
*/
public class AndroidXmlTranslator {
private static final String DEFAULT_CLASS_NAME = "AndroidManifest.xml";
public static String parse(InputStream apk) {
ZipInputStream is = null;
ByteArrayOutputStream bout = null;
try {
long size = apk.available();
ZipInputStream zip = new ZipInputStream(apk);
do {
ZipEntry entry = zip.getNextEntry();
if (entry.getName().equals(DEFAULT_CLASS_NAME)) {
is = zip;
}
} while (is == null);
bout = new ByteArrayOutputStream((int) size);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) > 0) {
bout.write(buffer, 0, bytesRead);
}
XmlDecompressor xmlDecompressor = new XmlDecompressor();
return xmlDecompressor.decompressXml(bout.toByteArray());
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
closeResource(is);
closeResource(bout);
}
}
private static void closeResource(Closeable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (IOException ex) {
System.err.println("Error closing resource: " + ex.getMessage());
ex.printStackTrace(System.err);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy