下面列出了java.util.jar.JarEntry#STORED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static JarEntry newJarEntry(Entry entry) {
final JarEntry result = new JarEntry(entry.getName());
if (result.getCreationTime() != null) {
result.setCreationTime(entry.getCreationTime());
}
if (result.getLastModifiedTime() != null) {
result.setLastModifiedTime(entry.getLastModifiedTime());
}
if (entry.getExtra() != null) {
result.setExtra(entry.getExtra());
}
if (result.getComment() != null) {
result.setComment(entry.getComment());
}
if (!entry.isDirectory()) {
final int method = entry.getMethod();
if (method == JarEntry.STORED || method == ZipEntry.DEFLATED) {
result.setMethod(method);
}
}
return result;
}
/**
* Writes an entry with specific contents to the jar. Directory entries must include the trailing
* '/'.
*/
protected void writeEntry(JarOutputStream out, String name, byte[] content) throws IOException {
if (names.add(name)) {
// Create a new entry
JarEntry entry = new JarEntry(name);
entry.setTime(newEntryTimeMillis(name));
int size = content.length;
entry.setSize(size);
if (size == 0) {
entry.setMethod(JarEntry.STORED);
entry.setCrc(0);
out.putNextEntry(entry);
} else {
entry.setMethod(storageMethod);
if (storageMethod == JarEntry.STORED) {
CRC32 crc = new CRC32();
crc.update(content);
entry.setCrc(crc.getValue());
}
out.putNextEntry(entry);
out.write(content);
}
out.closeEntry();
}
}
/**
* Writes a standard Java manifest entry into the JarOutputStream. This includes the directory
* entry for the "META-INF" directory
*
* @param content the Manifest content to write to the manifest entry.
* @throws IOException
*/
protected void writeManifestEntry(JarOutputStream out, byte[] content) throws IOException {
int oldStorageMethod = storageMethod;
// Do not compress small manifest files, the compressed one is frequently
// larger than the original. The threshold of 256 bytes is somewhat arbitrary.
if (content.length < 256) {
storageMethod = JarEntry.STORED;
}
try {
writeEntry(out, MANIFEST_DIR, new byte[] {});
writeEntry(out, MANIFEST_NAME, content);
} finally {
storageMethod = oldStorageMethod;
}
}
/**
* Copies file or directory entries from the file system into the jar. Directory entries will be
* detected and their names automatically '/' suffixed.
*/
protected void copyEntry(JarOutputStream out, String name, Path path) throws IOException {
if (!names.contains(name)) {
if (!Files.exists(path)) {
throw new FileNotFoundException(path.toAbsolutePath() + " (No such file or directory)");
}
boolean isDirectory = Files.isDirectory(path);
if (isDirectory && !name.endsWith("/")) {
name = name + '/'; // always normalize directory names before checking set
}
if (names.add(name)) {
if (verbose) {
System.err.println("adding " + path);
}
// Create a new entry
long size = isDirectory ? 0 : Files.size(path);
JarEntry outEntry = new JarEntry(name);
long newtime =
normalize ? normalizedTimestamp(name) : Files.getLastModifiedTime(path).toMillis();
outEntry.setTime(newtime);
outEntry.setSize(size);
if (size == 0L) {
outEntry.setMethod(JarEntry.STORED);
outEntry.setCrc(0);
out.putNextEntry(outEntry);
} else {
outEntry.setMethod(storageMethod);
if (storageMethod == JarEntry.STORED) {
// ZipFile requires us to calculate the CRC-32 for any STORED entry.
// It would be nicer to do this via DigestInputStream, but
// the architecture of ZipOutputStream requires us to know the CRC-32
// before we write the data to the stream.
byte[] bytes = Files.readAllBytes(path);
CRC32 crc = new CRC32();
crc.update(bytes);
outEntry.setCrc(crc.getValue());
out.putNextEntry(outEntry);
out.write(bytes);
} else {
out.putNextEntry(outEntry);
Files.copy(path, out);
}
}
out.closeEntry();
}
}
}
/**
* Enables or disables compression for the Jar file entries.
*
* @param compression if true enables compressions for the Jar file entries.
*/
public void setCompression(boolean compression) {
storageMethod = compression ? JarEntry.DEFLATED : JarEntry.STORED;
}