[英]How to delete a specific File/Folder from a jar pragmatically in java
如何在 java 中以務實的方式從 jar 中刪除特定的文件/文件夾。
我有一個 jar ABC.jar 包含文件、文件夾和另一個 jars 說 child.jar。 在 child.jar 下我想刪除一個特定的文件。 我能怎么做? 這樣我的 ABC.jar 結構保持不變。
任何幫助將不勝感激。
提前致謝。
正如@icza所回答的,我們必須遍歷原始jar文件並刪除我們不想要的條目。 這是您可以參考的Java代碼。
public static void main(String[] args) throws IOException {
String jarName = args[0];
String fileName = args[1];
// Create file descriptors for the jar and a temp jar.
File jarFile = new File(jarName);
File tempJarFile = new File(jarName + ".tmp");
// Open the jar file.
JarFile jar = new JarFile(jarFile);
System.out.println(jarName + " opened.");
// Initialize a flag that will indicate that the jar was updated.
boolean jarUpdated = false;
try {
// Create a temp jar file with no manifest. (The manifest will
// be copied when the entries are copied.)
Manifest jarManifest = jar.getManifest();
JarOutputStream tempJar =
new JarOutputStream(new FileOutputStream(tempJarFile));
// Allocate a buffer for reading entry data.
byte[] buffer = new byte[1024];
int bytesRead;
try {
// Open the given file.
FileInputStream file = new FileInputStream(fileName);
try {
// Create a jar entry and add it to the temp jar.
JarEntry entry = new JarEntry(fileName);
tempJar.putNextEntry(entry);
// Read the file and write it to the jar.
while ((bytesRead = file.read(buffer)) != -1) {
tempJar.write(buffer, 0, bytesRead);
}
System.out.println(entry.getName() + " added.");
}
finally {
file.close();
}
// Loop through the jar entries and add them to the temp jar,
// skipping the entry that was added to the temp jar already.
for (Enumeration entries = jar.entries(); entries.hasMoreElements(); ) {
// Get the next entry.
JarEntry entry = (JarEntry) entries.nextElement();
// If the entry has not been added already, add it.
if (! entry.getName().equals(fileName)) {
// Get an input stream for the entry.
InputStream entryStream = jar.getInputStream(entry);
// Read the entry and write it to the temp jar.
tempJar.putNextEntry(entry);
while ((bytesRead = entryStream.read(buffer)) != -1) {
tempJar.write(buffer, 0, bytesRead);
}
}
}
jarUpdated = true;
}
catch (Exception ex) {
System.out.println(ex);
// Add a stub entry here, so that the jar will close without an
// exception.
tempJar.putNextEntry(new JarEntry("stub"));
}
finally {
tempJar.close();
}
}
finally {
jar.close();
System.out.println(jarName + " closed.");
// If the jar was not updated, delete the temp jar file.
if (! jarUpdated) {
tempJarFile.delete();
}
}
// If the jar was updated, delete the original jar file and rename the
// temp jar file to the original name.
if (jarUpdated) {
jarFile.delete();
tempJarFile.renameTo(jarFile);
System.out.println(jarName + " updated.");
}
}
Jar / zip文件不可編輯。 您無法從jar / zip文件中刪除條目。
你可以做的是“重新創建”這樣的jar文件:啟動一個新的jar文件,迭代當前jar文件的條目,並將這些條目添加到你不想刪除的新jar文件中。
從理論上講,可以刪除這樣的條目(但標准的Java lib不適合這個,意思是ZipFile
, ZipInputStream
, JarFile
, JarInputStream
類):
jar / zip文件中的條目是順序的。 每個條目都有一個標題,其中包含有關條目的信息(可以是文件或文件夾條目)。 此標頭還包含條目的字節長度。 因此,您可以按順序迭代條目,如果遇到要刪除的條目(並且您從其標題中知道其大小),則可以將文件的剩余部分(此條目后的內容)復制到文本的開頭。當前條目(顯然文件大小應該由當前/刪除條目的長度確定)。
或者您的其他選項包括不通過Java執行此操作,而是使用zip -d
命令本身等外部工具。
Sun / Oracle錯誤數據庫要求在java api中實現此功能。
點擊這里
有一種通過在運行時執行命令shell從JAR中刪除文件的簡單方法。 執行以下命令可以完成以下任務:
Runtime.getRuntime().exec("zip -d path\my.jar some_file.txt");
其中path是jar文件的絕對路徑, some_file.txt是要刪除的文件。 在此示例中,文件駐留在jar的主文件夾中。 如果文件位於其他文件夾中,則可能需要提供其相對路徑
你可能知道jar本身的路徑,或者可以根據你執行命令shell的類來找到它的路徑:
String path = SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
您可以通過偵聽可用流來跟蹤進程的執行:
Process p = Runtime.getRuntime().exec("zip -d path\my.jar some_file.txt");
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine())!= null) {
sb.append(line + "\n");
System.out.println(line);
}
System.out.println(sb.toString());
public static void filterJar(Path jarInFileName, String skipRegex, Path jarOutFileName) throws IOException {
ZipEntry entry;
ZipInputStream zis = null;
JarOutputStream os = null;
FileInputStream is = null;
try {
is = new FileInputStream(jarInFileName.toFile());
Pattern pattern = Pattern.compile(skipRegex);
zis = new ZipInputStream(is);
os = new JarOutputStream(new FileOutputStream(jarOutFileName.toFile()));
while ((entry = zis.getNextEntry()) != null) {
if (pattern.matcher(entry.getName()).matches()) continue;
os.putNextEntry(entry);
if (!entry.isDirectory()) {
byte[] bytes = toBytes(zis);
os.write(bytes);
}
}
}
catch (Exception ex) {
throw new IOException("unable to filter jar:" + ex.getMessage());
}
finally {
closeQuietly(is);
closeQuietly(os);
}
}
public static void closeQuietly(final Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
}
catch (final Exception e) {}
}
public static byte[] toBytes(InputStream aInput) throws IOException {
byte[] bucket = new byte[5 * 1024];
ByteArrayOutputStream result = null;
result = new ByteArrayOutputStream(bucket.length);
int bytesRead = 0;
while (bytesRead != -1) {
bytesRead = aInput.read(bucket);
if (bytesRead > 0) {
result.write(bucket, 0, bytesRead);
}
}
return result.toByteArray();
}
public static void main(String[] args) throws IOException {
filterJar(Paths.get("./old.jar"), "BOOT-INF/lib.*", Paths.get("./new.jar"));
}
您可以使用 Zip 文件系統將 zip/jar 文件視為文件系統。 這將允許您編輯、刪除和向 jar 文件添加文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.