Java by API/java.util.jar/JarOutputStream
Версия от 17:43, 31 мая 2010; (обсуждение)
JarOutputStream: putNextEntry(ZipEntry ze)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
public class Main {
public static int BUFFER_SIZE = 10240;
void createJarArchive(File archiveFile, File[] tobeJared) throws Exception {
byte buffer[] = new byte[BUFFER_SIZE];
FileOutputStream stream = new FileOutputStream(archiveFile);
JarOutputStream out = new JarOutputStream(stream, new Manifest());
for (int i = 0; i < tobeJared.length; i++) {
if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory())
continue; // Just in case...
JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
jarAdd.setTime(tobeJared[i].lastModified());
out.putNextEntry(jarAdd);
FileInputStream in = new FileInputStream(tobeJared[i]);
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0)
break;
out.write(buffer, 0, nRead);
}
in.close();
}
out.close();
stream.close();
}
}
new JarOutputStream(OutputStream out)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;
import java.util.zip.GZIPInputStream;
public class Main {
public static void main(String[] args) throws Exception {
String inName = args[0];
String outName;
if (inName.endsWith(".pack.gz")) {
outName = inName.substring(0, inName.length() - 8);
} else if (inName.endsWith(".pack")) {
outName = inName.substring(0, inName.length() - 5);
} else {
outName = inName + ".unpacked";
}
JarOutputStream out = null;
InputStream in = null;
Pack200.Unpacker unpacker = Pack200.newUnpacker();
out = new JarOutputStream(new FileOutputStream(outName));
in = new FileInputStream(inName);
if (inName.endsWith(".gz"))
in = new GZIPInputStream(in);
unpacker.unpack(in, out);
out.close();
}
}