Java 更新.JAR';从代码中删除内容
我正在制作一个需要更新的游戏 我有两个JAR文件:Java 更新.JAR';从代码中删除内容,java,jar,Java,Jar,我正在制作一个需要更新的游戏 我有两个JAR文件:Update.JAR和Game.JAR 基本上,我希望能够修改Game.Jar而不完全覆盖它 我想: 从代码中以Zip文件的形式打开Jar文件 替换/添加一些资源 重新打包Jar文件 有没有一个简单的方法或类可以做到这一点?如果不是,最干净的方法是什么?Java JAR文件是普通的ZIP文件。因此,您可以使用处理拉链的代码打开和修改它 下面是一个有用的片段(由David提供): 导入java.io.File; 导入java.io.FileInpu
Update.JAR
和Game.JAR
基本上,我希望能够修改Game.Jar而不完全覆盖它
我想:
有没有一个简单的方法或类可以做到这一点?如果不是,最干净的方法是什么?Java JAR文件是普通的ZIP文件。因此,您可以使用处理拉链的代码打开和修改它 下面是一个有用的片段(由David提供):
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipInputStream;
导入java.util.zip.ZipoutStream;
公共类更新程序{
公共静态void main(字符串[]args){
File[]contents={新文件(“F:\\ResourceTest.txt”),
新文件(“F:\\ResourceTest2.bmp”)};
File jarFile=新文件(“F:\\RepackMe.jar”);
试一试{
updateZipFile(jarFile,contents);
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共静态void updateZipFile(文件zipFile,
文件[]文件)引发IOException{
//获取临时文件
File tempFile=File.createTempFile(zipFile.getName(),null);
//删除它,否则无法将现有zip重命名为它。
tempFile.delete();
布尔renameOk=zipFile.renameTo(tempFile);
如果(!renameOk)
{
抛出新的RuntimeException(“无法将文件“+zipFile.getAbsolutePath()+”重命名为“+tempFile.getAbsolutePath()”);
}
字节[]buf=新字节[1024];
ZipInputStream zin=新的ZipInputStream(新文件输入流(tempFile));
ZipOutputStream out=newzipoutpstream(newfileoutputstream(zipFile));
ZipEntry entry=zin.getNextEntry();
while(条目!=null){
String name=entry.getName();
布尔notInFiles=true;
用于(文件f:文件){
如果(f.getName().equals(name)){
notInFiles=false;
打破
}
}
如果(未填充){
//将ZIP条目添加到输出流。
out.putNextEntry(新ZipEntry(名称));
//将字节从ZIP文件传输到输出文件
内伦;
而((len=zin.read(buf))>0){
out.write(buf,0,len);
}
}
entry=zin.getnextery();
}
//关闭溪流
zin.close();
//压缩文件
对于(int i=0;i0){
out.write(buf,0,len);
}
//完成条目
out.closeEntry();
in.close();
}
//完成ZIP文件
out.close();
tempFile.delete();
}
}
在Java 7中,最好的方法是使用内置的Zip文件系统提供程序:
i、 e
import java.util.*;
导入java.net.URI;
导入java.nio.file.Path;
导入java.nio.file.*;
公共类ZipFSPUser{
公共静态void main(字符串[]args)抛出可丢弃的{
Map env=new HashMap();
环境出售(“创建”、“真实”);
//使用语法定位文件系统
//在java.net.JarURLConnection中定义
URI=URI.create(“jar:file:/codeSamples/zipfs/zipfstest.zip”);
try(FileSystem-zipfs=FileSystems.newFileSystem(uri,env)){
Path externalTxtFile=Path.get(“/codeSamples/zipfs/SomeTextFile.txt”);
Path pathInZipfile=zipfs.getPath(“/SomeTextFile.txt”);
//将文件复制到zip文件中
复制(externalTxtFile、pathInZipfile、,
StandardCopyOption.REPLACE_现有);
}
}
}
~此外,
java.util.jar
和java.util.zip
包包含用于执行这些类型操作的类。-1作者明确表示,他知道的和你告诉他的一样多。我很确定zip不在最初的帖子中,我猜它是在发布后的5分钟宽限期内编辑的。@Lucero它在第一篇文章中。好吧,很抱歉忽略了它。你想让我删除答案吗?看看这个[帖子][1],看看它是否有用。[1]:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class JarUpdater {
public static void main(String[] args) {
File[] contents = {new File("F:\\ResourceTest.txt"),
new File("F:\\ResourceTest2.bmp")};
File jarFile = new File("F:\\RepackMe.jar");
try {
updateZipFile(jarFile, contents);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void updateZipFile(File zipFile,
File[] files) throws IOException {
// get a temp file
File tempFile = File.createTempFile(zipFile.getName(), null);
// delete it, otherwise you cannot rename your existing zip to it.
tempFile.delete();
boolean renameOk=zipFile.renameTo(tempFile);
if (!renameOk)
{
throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath());
}
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
ZipEntry entry = zin.getNextEntry();
while (entry != null) {
String name = entry.getName();
boolean notInFiles = true;
for (File f : files) {
if (f.getName().equals(name)) {
notInFiles = false;
break;
}
}
if (notInFiles) {
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(name));
// Transfer bytes from the ZIP file to the output file
int len;
while ((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
entry = zin.getNextEntry();
}
// Close the streams
zin.close();
// Compress the files
for (int i = 0; i < files.length; i++) {
InputStream in = new FileInputStream(files[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(files[i].getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
tempFile.delete();
}
}
import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;
public class ZipFSPUser {
public static void main(String [] args) throws Throwable {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
// locate file system by using the syntax
// defined in java.net.JarURLConnection
URI uri = URI.create("jar:file:/codeSamples/zipfs/zipfstest.zip");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
Path externalTxtFile = Paths.get("/codeSamples/zipfs/SomeTextFile.txt");
Path pathInZipfile = zipfs.getPath("/SomeTextFile.txt");
// copy a file into the zip file
Files.copy( externalTxtFile,pathInZipfile,
StandardCopyOption.REPLACE_EXISTING );
}
}
}