下面列出了java.util.jar.JarInputStream#read ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
/**
* Merges two jars. The first jar will get merged into the output jar.
* A running set of jar entries is kept so that duplicates are skipped.
* This has the side-effect that the first instance of a given entry will be added
* and all subsequent entries are skipped.
*
* @param jin The input jar
* @param jout The output jar
* @param entries The set of existing entries. Note that this set will be mutated as part of this call.
* @return The set of entries.
* @throws IOException
*/
private Set<String> copy(JarInputStream jin, JarOutputStream jout, Set<String> entries) throws IOException {
byte[] buffer = new byte[1024];
for(JarEntry entry = jin.getNextJarEntry(); entry != null; entry = jin.getNextJarEntry()) {
if(entries.contains(entry.getName())) {
continue;
}
LOG.debug("Merging jar entry {}", entry.getName());
entries.add(entry.getName());
jout.putNextEntry(entry);
int len = 0;
while( (len = jin.read(buffer)) > 0 ) {
jout.write(buffer, 0, len);
}
}
return entries;
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
public byte[] extractEntry(String entry) throws IOException {
JarInputStream zin = new JarInputStream(new BufferedInputStream(new FileInputStream(jarFile)));
JarEntry currentEntry = null;
while ((currentEntry = zin.getNextJarEntry()) != null) {
if (currentEntry.getName().equals(entry)) {
// currentEntry.getSize() may not be accurate, so read bytes into a stream first
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
while (true) {
int n = zin.read(buf);
if (n < 0){
break;
}
baos.write(buf, 0, n);
}
zin.close();
return baos.toByteArray();
}
}
zin.close();
return null;
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
if (in.getManifest() != null) {
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
out.putNextEntry(me);
in.getManifest().write(out);
out.closeEntry();
}
byte[] buffer = new byte[1 << 14];
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
out.putNextEntry(je);
for (int nr; 0 < (nr = in.read(buffer)); ) {
out.write(buffer, 0, nr);
}
}
in.close();
markJarFile(out); // add PACK200 comment
}
/**
* Given an InputStream on a jar file, unjars the contents into the given
* directory.
*/
public void unjar(InputStream in, File destDir) throws IOException {
BufferedOutputStream dest = null;
JarInputStream jis = new JarInputStream(in);
JarEntry entry;
while ((entry = jis.getNextJarEntry()) != null) {
if (entry.isDirectory()) {
File dir = new File(destDir, entry.getName());
dir.mkdir();
if (entry.getTime() != -1) {
dir.setLastModified(entry.getTime());
}
continue;
}
int count;
byte[] data = new byte[BUFFER_SIZE];
File destFile = new File(destDir, entry.getName());
if (mVerbose) {
System.out.println("unjarring " + destFile +
" from " + entry.getName());
}
FileOutputStream fos = new FileOutputStream(destFile);
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
try {
while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
} finally {
dest.close();
}
if (entry.getTime() != -1) {
destFile.setLastModified(entry.getTime());
}
}
jis.close();
}
/**
* In this case we are dealing with facts which are not on the systems classpath.
*/
@Test
public void testSerializabilityWithJarFacts() throws Exception {
MapBackedClassLoader loader = new MapBackedClassLoader( this.getClass().getClassLoader() );
JarInputStream jis = new JarInputStream( this.getClass().getResourceAsStream( "/billasurf.jar" ) );
JarEntry entry = null;
byte[] buf = new byte[1024];
int len = 0;
while ( (entry = jis.getNextJarEntry()) != null ) {
if ( !entry.isDirectory() ) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ( (len = jis.read( buf )) >= 0 ) {
out.write( buf,
0,
len );
}
loader.addResource( entry.getName(),
out.toByteArray() );
}
}
String drl = "package foo.bar \n" +
"import com.billasurf.Board\n" +
"rule 'MyGoodRule' \n dialect 'mvel' \n when " +
" Board() " +
"then \n" +
" System.err.println(42); \n" +
"end\n";
KnowledgeBuilderConfiguration kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);
Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(kbuilderConf, drl);
kpkgs = SerializationHelper.serializeObject( kpkgs, loader );
}
private byte[] getBytes(JarInputStream jis) throws IOException {
int len = 0;
byte[] bytes = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
while ((len = jis.read(bytes, 0, bytes.length)) != -1) {
baos.write(bytes, 0, len);
}
return baos.toByteArray();
}
/**
* Fetches the game jar and loads and patches the classes
*
* @param jarURL The URL of the jar to be loaded and patched
* @return If no exceptions occurred
*/
public boolean fetch(String jarURL) {
Logger.Info("Fetching Jar: " + jarURL);
try {
JarInputStream in = new JarInputStream(Settings.getResourceAsStream(jarURL));
Launcher.getInstance().setProgress(1, 1);
JarEntry entry;
while ((entry = in.getNextJarEntry()) != null) {
// Check if file is needed
String name = entry.getName();
// Read class to byte array
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int readSize;
while ((readSize = in.read(data, 0, data.length)) != -1) bOut.write(data, 0, readSize);
byte[] classData = bOut.toByteArray();
bOut.close();
Logger.Info("Loading file: " + name);
Launcher.getInstance().setStatus("Loading " + name + "...");
if (name.endsWith(".class")) {
name = name.substring(0, name.indexOf(".class"));
classData = JClassPatcher.getInstance().patch(classData);
m_classData.put(name, classData);
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private static File rewrite(File jar, String[] mavenCP, String classpath) throws IOException { // #190992
String[] classpathEntries = tokenizePath(classpath);
StringBuilder classPathHeader = new StringBuilder();
for (String artifact : mavenCP) {
String[] grpArtVers = artifact.split(":");
String partOfPath = File.separatorChar + grpArtVers[0].replace('.', File.separatorChar) + File.separatorChar + grpArtVers[1] + File.separatorChar + grpArtVers[2] + File.separatorChar + grpArtVers[1] + '-' + grpArtVers[2];
File dep = null;
for (String classpathEntry : classpathEntries) {
if (classpathEntry.endsWith(".jar") && classpathEntry.contains(partOfPath)) {
dep = new File(classpathEntry);
break;
}
}
if (dep == null) {
throw new IOException("no match for " + artifact + " found in " + classpath);
}
File depCopy = File.createTempFile(artifact.replace(':', '-') + '-', ".jar");
depCopy.deleteOnExit();
NbTestCase.copytree(dep, depCopy);
if (classPathHeader.length() > 0) {
classPathHeader.append(' ');
}
classPathHeader.append(depCopy.getName());
}
String n = jar.getName();
int dot = n.lastIndexOf('.');
File jarCopy = File.createTempFile(n.substring(0, dot) + '-', n.substring(dot));
jarCopy.deleteOnExit();
InputStream is = new FileInputStream(jar);
try {
OutputStream os = new FileOutputStream(jarCopy);
try {
JarInputStream jis = new JarInputStream(is);
Manifest mani = new Manifest(jis.getManifest());
mani.getMainAttributes().putValue("Class-Path", classPathHeader.toString());
JarOutputStream jos = new JarOutputStream(os, mani);
JarEntry entry;
while ((entry = jis.getNextJarEntry()) != null) {
if (entry.getName().matches("META-INF/.+[.]SF")) {
throw new IOException("cannot handle signed JARs");
}
jos.putNextEntry(entry);
byte[] buf = new byte[4092];
for (;;) {
int more = jis.read(buf, 0, buf.length);
if (more == -1) {
break;
}
jos.write(buf, 0, more);
}
}
jis.close();
jos.close();
} finally {
os.close();
}
} finally {
is.close();
}
return jarCopy;
}
public static String generateUnhide(Context context, File output) {
File temp = new File(context.getCacheDir(), "temp.apk");
String pkg = "";
Log.d(TAG, "try start ");
try {
JarInputStream source = new JarInputStream(new FileInputStream(new File(Tools.runCommand("pm path " + ISU_APK + "| head -n1 | cut -d: -f2", Tools.SuBinary(), context))));
JarOutputStream dest = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(temp)));
JarEntry entry;
int size;
byte buffer[] = new byte[4096];
while ((entry = source.getNextJarEntry()) != null) {
dest.putNextEntry(new JarEntry(entry.getName()));
if (TextUtils.equals(entry.getName(), ANDROID_MANIFEST)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((size = source.read(buffer)) > 0) {
baos.write(buffer, 0, size);
}
byte xml[] = baos.toByteArray();
boolean need_zero = false;
int offset = Tools.FindOffSet(xml, COM_PKG_NAME);
if (offset < 0) {
Log.d(TAG, "offset < 0 try appModZeros");
byte[] COM_PKG_NAME_ZERO = (Tools.appStringAddZeros(app_name)).getBytes();
offset = Tools.FindOffSet(xml, COM_PKG_NAME_ZERO);
need_zero = true;
}
if (offset < 0) {
Log.d(TAG, "offset < 0 again return");
return "";
}
Log.d(TAG, "offset " + offset + " leg " + COM_PKG_NAME.length);
// Patch binary XML with new package name
if (Tools.appInstaled(context)) {
if (need_zero)
pkg = Tools.appStringAddZeros(Tools.readString("hide_app_name", null, context));
else
pkg = Tools.readString("hide_app_name", null, context);
} else
pkg = Tools.appStringMod(app_name, need_zero);
System.arraycopy(pkg.getBytes(), 0, xml, offset, pkg.length());
dest.write(xml);
} else {
while ((size = source.read(buffer)) > 0) {
dest.write(buffer, 0, size);
}
}
}
source.close();
dest.close();
signZip(context, temp, output, false);
temp.delete();
} catch (IOException e) {
e.printStackTrace();
return pkg;
}
Log.d(TAG, "pkg " + pkg.replace("\0", ""));
return pkg;
}
/**
* Reads the content of the current {@link JarEntry} from the given {@link JarInputStream} into a byte array.
* @param _jis
* @return
* @throws IOException
*/
private byte[] readContent(JarInputStream _jis) throws IOException {
byte[] bytes = new byte[1024];
while(_jis.read(bytes, 0, 1024)!=-1) {;} //read()
return bytes;
}