


How to Avoid Undocumented Quirks When Using JarOutputStream to Create JAR Files?
Creating JAR Files Using JarOutputStream
To generate a JAR file programmatically, JarOutputStream is often employed. However, it is essential to avoid certain undocumented quirks within JarOutputStream:
1. Directories Ending with a Slash:
Directory names in JAR files must end with a '/' slash.
2. Paths Using Forward Slashes:
Use forward slashes '/' in paths, as opposed to backslashes ''.
3. No Leading Slashes in Entry Names:
Entry names should not start with a '/' slash.
Example Code with Corrections:
The following corrected code constructs a valid JAR file with a manifest file:
<code class="java">public void run() throws IOException { Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest); add(new File("inputDirectory"), target); target.close(); } private void add(File source, JarOutputStream target) throws IOException { String name = source.getPath().replace("\", "/"); if (source.isDirectory()) { if (!name.endsWith("/")) { name += "/"; } JarEntry entry = new JarEntry(name); entry.setTime(source.lastModified()); target.putNextEntry(entry); target.closeEntry(); for (File nestedFile : source.listFiles()) { add(nestedFile, target); } } else { JarEntry entry = new JarEntry(name); entry.setTime(source.lastModified()); target.putNextEntry(entry); try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(source))) { byte[] buffer = new byte[1024]; while (true) { int count = in.read(buffer); if (count == -1) { break; } target.write(buffer, 0, count); } target.closeEntry(); } } }</code>
The above is the detailed content of How to Avoid Undocumented Quirks When Using JarOutputStream to Create JAR Files?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools