Skip to content

Commit

Permalink
Improved unpacking natives jar
Browse files Browse the repository at this point in the history
  • Loading branch information
Maescool committed Dec 2, 2012
1 parent 39ef8f0 commit 610263b
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions src/com/mojang/mojam/downloader/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.io.*;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.mojang.mojam.MojamComponent;
import com.mojang.mojam.MojamStartup;
Expand Down Expand Up @@ -431,39 +431,51 @@ public static String getMD5Checksum(String filename) throws Exception {

// unpack jar

public static void unpackJar(String jarFile, String destDir) {
DownloadScreen.unpackStart(new File(jarFile).getName().toString());
public static boolean unpackJar(String jarFile, String destDir) {

File jarFileJar = new File(jarFile);
File destDirDir = new File(destDir);

if (!destDirDir.isDirectory()) {
destDirDir.mkdirs();
}

FileInputStream input;
try {
input = new FileInputStream(jarFileJar);
} catch (FileNotFoundException e) { return false; }

ZipInputStream zipIn = new ZipInputStream(input);
try {
JarFile jar = new JarFile(jarFile);
Enumeration<JarEntry> enumi = jar.entries();

while (enumi.hasMoreElements()) {
JarEntry file = (JarEntry) enumi.nextElement();
File f = new File(destDir + File.separator + file.getName());
if (file.isDirectory()) { // if its a directory, create it
f.mkdir();
ZipEntry currentEntry = zipIn.getNextEntry();
while (currentEntry != null) {
if (currentEntry.getName().contains("META-INF")) {
currentEntry = zipIn.getNextEntry();
continue;
}
InputStream is = jar.getInputStream(file); // get the
// input
// stream
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[1 << Options.getAsInteger(Options.DLBUFFERSIZE, 13)];
System.out.println(buffer.length);
int read;

while ((read = is.read(buffer)) != -1) {
fos.write(buffer, 0, read);

FileOutputStream outStream = new FileOutputStream(new File(destDirDir, currentEntry.getName()));

int readLen;
byte[] buffer = new byte[1024];
while ((readLen = zipIn.read(buffer, 0, buffer.length)) > 0) {
outStream.write(buffer, 0, readLen);
}
fos.close();
is.close();
outStream.close();

currentEntry = zipIn.getNextEntry();
}
jar.close();
} catch (Exception e) {
DownloadScreen.unpackStop();
e.printStackTrace();
} catch (IOException e) {
return false;
} finally {
try {
zipIn.close();
input.close();
} catch (IOException e) { }
}
DownloadScreen.unpackStop();

jarFileJar.delete();
return true;
}

// download
Expand Down

0 comments on commit 610263b

Please sign in to comment.