本文整理汇总了Java中java.util.jar.JarEntry.setSize方法的典型用法代码示例。如果您正苦于以下问题:Java JarEntry.setSize方法的具体用法?Java JarEntry.setSize怎么用?Java JarEntry.setSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.jar.JarEntry
的用法示例。
在下文中一共展示了JarEntry.setSize方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeJarEntry
import java.util.jar.JarEntry; //导入方法依赖的package包/类
private static void writeJarEntry(JarOutputStream jos, String path, File f) throws IOException, FileNotFoundException {
JarEntry je = new JarEntry(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new FileInputStream(f);
try {
copyStreams(is, baos);
} finally {
is.close();
}
byte[] data = baos.toByteArray();
je.setSize(data.length);
CRC32 crc = new CRC32();
crc.update(data);
je.setCrc(crc.getValue());
jos.putNextEntry(je);
jos.write(data);
}
示例2: createJar
import java.util.jar.JarEntry; //导入方法依赖的package包/类
/**
* Create a fresh JAR file.
* @param jar the file to create
* @param contents keys are JAR entry paths, values are text contents (will be written in UTF-8)
* @param manifest a manifest to store (or null for none)
* @deprecated use {@link JarBuilder} instead
*/
@Deprecated
public static void createJar(File jar, Map<String,String> contents, Manifest manifest) throws IOException {
if (manifest != null) {
manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); // workaround for JDK bug
}
jar.getParentFile().mkdirs();
OutputStream os = new FileOutputStream(jar);
try {
JarOutputStream jos = manifest != null ? new JarOutputStream(os, manifest) : new JarOutputStream(os);
for (Map.Entry<String,String> entry : contents.entrySet()) {
String path = entry.getKey();
byte[] data = entry.getValue().getBytes("UTF-8");
JarEntry je = new JarEntry(path);
je.setSize(data.length);
CRC32 crc = new CRC32();
crc.update(data);
je.setCrc(crc.getValue());
jos.putNextEntry(je);
jos.write(data);
}
jos.close();
} finally {
os.close();
}
}
示例3: createJar
import java.util.jar.JarEntry; //导入方法依赖的package包/类
/**
* Create a fresh JAR file.
* @param jar the file to create
* @param contents keys are JAR entry paths, values are text contents (will be written in UTF-8)
* @param manifest a manifest to store (key/value pairs for main section)
*/
public static void createJar(File jar, Map<String,String> contents, Map<String,String> manifest) throws IOException {
// XXX use TestFileUtils.writeZipFile
Manifest m = new Manifest();
m.getMainAttributes().putValue("Manifest-Version", "1.0"); // workaround for JDK bug
for (Map.Entry<String,String> line : manifest.entrySet()) {
m.getMainAttributes().putValue(line.getKey(), line.getValue());
}
jar.getParentFile().mkdirs();
OutputStream os = new FileOutputStream(jar);
try {
JarOutputStream jos = new JarOutputStream(os, m);
Iterator it = contents.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String path = (String) entry.getKey();
byte[] data = ((String) entry.getValue()).getBytes("UTF-8");
JarEntry je = new JarEntry(path);
je.setSize(data.length);
CRC32 crc = new CRC32();
crc.update(data);
je.setCrc(crc.getValue());
jos.putNextEntry(je);
jos.write(data);
}
jos.close();
} finally {
os.close();
}
}
示例4: testJarMapping
import java.util.jar.JarEntry; //导入方法依赖的package包/类
/**
* Check that jar: URLs are correctly mapped back into JarFileSystem resources.
* @see "#39190"
*/
public void testJarMapping() throws Exception {
clearWorkDir();
File workdir = getWorkDir();
File jar = new File(workdir, "test.jar");
String textPath = "x.txt";
OutputStream os = new FileOutputStream(jar);
try {
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(ZipEntry.STORED);
JarEntry entry = new JarEntry(textPath);
entry.setSize(0L);
entry.setTime(System.currentTimeMillis());
entry.setCrc(new CRC32().getValue());
jos.putNextEntry(entry);
jos.flush();
jos.close();
} finally {
os.close();
}
assertTrue("JAR was created", jar.isFile());
assertTrue("JAR is not empty", jar.length() > 0L);
JarFileSystem jfs = new JarFileSystem();
jfs.setJarFile(jar);
Repository.getDefault().addFileSystem(jfs);
FileObject rootFO = jfs.getRoot();
FileObject textFO = jfs.findResource(textPath);
assertNotNull("JAR contains a/b.txt", textFO);
String rootS = "jar:" + BaseUtilities.toURI(jar) + "!/";
URL rootU = new URL(rootS);
URL textU = new URL(rootS + textPath);
assertEquals("correct FO -> URL for root", rootU, URLMapper.findURL(rootFO, URLMapper.EXTERNAL));
assertEquals("correct FO -> URL for " + textPath, textU, URLMapper.findURL(textFO, URLMapper.EXTERNAL));
assertTrue("correct URL -> FO for root", Arrays.asList(URLMapper.findFileObjects(rootU)).contains(rootFO));
assertTrue("correct URL -> FO for " + textPath, Arrays.asList(URLMapper.findFileObjects(textU)).contains(textFO));
}
示例5: writeJarredFile
import java.util.jar.JarEntry; //导入方法依赖的package包/类
private static void writeJarredFile(JarOutputStream jos, String file, String suffix, byte[] bytes) {
String fileName = file.replace(".", "/") + "." + suffix;
JarEntry ze = new JarEntry(fileName);
try {
ze.setSize(bytes.length);
jos.putNextEntry(ze);
jos.write(bytes);
jos.closeEntry();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例6: createJar
import java.util.jar.JarEntry; //导入方法依赖的package包/类
/**
* Creates a jar file from the resources (including dex file arrays).
*
* @param fileName {@code non-null;} name of the file
* @return whether the creation was successful
*/
private boolean createJar(String fileName) {
/*
* Make or modify the manifest (as appropriate), put the dex
* array into the resources map, and then process the entire
* resources map in a uniform manner.
*/
try {
Manifest manifest = makeManifest();
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
try {
for (Map.Entry<String, byte[]> e :
outputResources.entrySet()) {
String name = e.getKey();
byte[] contents = e.getValue();
JarEntry entry = new JarEntry(name);
int length = contents.length;
if (args.verbose) {
DxConsole.out.println("writing " + name + "; size " + length + "...");
}
entry.setSize(length);
jarOut.putNextEntry(entry);
jarOut.write(contents);
jarOut.closeEntry();
}
} finally {
jarOut.finish();
jarOut.flush();
closeOutput(out);
}
} catch (Exception ex) {
if (args.debug) {
DxConsole.err.println("\ntrouble writing output:");
ex.printStackTrace(DxConsole.err);
} else {
DxConsole.err.println("\ntrouble writing output: " +
ex.getMessage());
}
return false;
}
return true;
}
示例7: createJar
import java.util.jar.JarEntry; //导入方法依赖的package包/类
/**
* Creates a jar file from the resources (including dex file arrays).
*
* @param fileName {@code non-null;} name of the file
* @return whether the creation was successful
*/
private static boolean createJar(String fileName) {
/*
* Make or modify the manifest (as appropriate), put the dex
* array into the resources map, and then process the entire
* resources map in a uniform manner.
*/
try {
Manifest manifest = makeManifest();
OutputStream out = openOutput(fileName);
JarOutputStream jarOut = new JarOutputStream(out, manifest);
try {
for (Map.Entry<String, byte[]> e :
outputResources.entrySet()) {
String name = e.getKey();
byte[] contents = e.getValue();
JarEntry entry = new JarEntry(name);
int length = contents.length;
if (args.verbose) {
DxConsole.out.println("writing " + name + "; size " + length + "...");
}
entry.setSize(length);
jarOut.putNextEntry(entry);
jarOut.write(contents);
jarOut.closeEntry();
}
} finally {
jarOut.finish();
jarOut.flush();
closeOutput(out);
}
} catch (Exception ex) {
if (args.debug) {
DxConsole.err.println("\ntrouble writing output:");
ex.printStackTrace(DxConsole.err);
} else {
DxConsole.err.println("\ntrouble writing output: " +
ex.getMessage());
}
return false;
}
return true;
}
示例8: unpackSegment
import java.util.jar.JarEntry; //导入方法依赖的package包/类
private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0");
// Process the output directory or jar output.
new PackageReader(pkg, in).read();
if (props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
if (props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50");
pkg.ensureAllClassFiles();
// Now write out the files.
Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses());
for (Package.File file : pkg.getFiles()) {
String name = file.nameString;
JarEntry je = new JarEntry(Utils.getJarEntryName(name));
boolean deflate;
deflate = (keepDeflateHint)
? (((file.options & Constants.FO_DEFLATE_HINT) != 0) ||
((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
: deflateHint;
boolean needCRC = !deflate; // STORE mode requires CRC
if (needCRC) crc.reset();
bufOut.reset();
if (file.isClassStub()) {
Package.Class cls = file.getStubClass();
assert(cls != null);
new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
classesToWrite.remove(cls); // for an error check
} else {
// collect data & maybe CRC
file.writeTo(needCRC ? crcOut : bufOut);
}
je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
if (needCRC) {
if (verbose > 0)
Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue());
je.setMethod(JarEntry.STORED);
je.setSize(bufOut.size());
je.setCrc(crc.getValue());
}
if (keepModtime) {
je.setTime(file.modtime);
// Convert back to milliseconds
je.setTime((long)file.modtime * 1000);
} else {
je.setTime((long)modtime * 1000);
}
out.putNextEntry(je);
bufOut.writeTo(out);
out.closeEntry();
if (verbose > 0)
Utils.log.info("Writing "+Utils.zeString((ZipEntry)je));
}
assert(classesToWrite.isEmpty());
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100");
pkg.reset(); // reset for the next segment, if any
}
示例9: main
import java.util.jar.JarEntry; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
String srcDir = System.getProperty("test.src", ".");
String keystore = srcDir + "/JarSigning.keystore";
String jarName = "largeJarEntry.jar";
// Set java.io.tmpdir to the current working dir (see 6474350)
System.setProperty("java.io.tmpdir", System.getProperty("user.dir"));
// first, create jar file with 8M uncompressed entry
// note, we set the max heap size to 8M in @run tag above
byte[] bytes = new byte[1000000];
CRC32 crc = new CRC32();
for (int i=0; i<8; i++) {
crc.update(bytes);
}
JarEntry je = new JarEntry("large");
je.setSize(8000000l);
je.setMethod(JarEntry.STORED);
je.setCrc(crc.getValue());
File file = new File(jarName);
FileOutputStream os = new FileOutputStream(file);
JarOutputStream jos = new JarOutputStream(os);
jos.setMethod(JarEntry.STORED);
jos.putNextEntry(je);
for (int i=0; i<8; i++) {
jos.write(bytes, 0, bytes.length);
}
jos.close();
String[] jsArgs = { "-keystore", keystore, "-storepass", "bbbbbb",
jarName, "b" };
// now, try to sign it
try {
sun.security.tools.jarsigner.Main.main(jsArgs);
} catch (OutOfMemoryError err) {
throw new Exception("Test failed with OutOfMemoryError", err);
} finally {
// remove jar file
file.delete();
}
}
示例10: unpackSegment
import java.util.jar.JarEntry; //导入方法依赖的package包/类
private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0");
// Process the output directory or jar output.
new PackageReader(pkg, in).read();
if (props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
if (props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50");
pkg.ensureAllClassFiles();
// Now write out the files.
Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses());
for (Package.File file : pkg.getFiles()) {
String name = file.nameString;
JarEntry je = new JarEntry(Utils.getJarEntryName(name));
boolean deflate;
deflate = (keepDeflateHint)
? (((file.options & Constants.FO_DEFLATE_HINT) != 0) ||
((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
: deflateHint;
boolean needCRC = !deflate; // STORE mode requires CRC
if (needCRC) crc.reset();
bufOut.reset();
if (file.isClassStub()) {
Package.Class cls = file.getStubClass();
assert(cls != null);
new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
classesToWrite.remove(cls); // for an error check
} else {
// collect data & maybe CRC
file.writeTo(needCRC ? crcOut : bufOut);
}
je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
if (needCRC) {
if (verbose > 0)
Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue());
je.setMethod(JarEntry.STORED);
je.setSize(bufOut.size());
je.setCrc(crc.getValue());
}
if (keepModtime) {
LocalDateTime ldt = LocalDateTime
.ofEpochSecond(file.modtime, 0, ZoneOffset.UTC);
je.setTimeLocal(ldt);
} else {
je.setTime((long)modtime * 1000);
}
out.putNextEntry(je);
bufOut.writeTo(out);
out.closeEntry();
if (verbose > 0)
Utils.log.info("Writing "+Utils.zeString((ZipEntry)je));
}
assert(classesToWrite.isEmpty());
props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100");
pkg.reset(); // reset for the next segment, if any
}