📄 decompression.java
字号:
import java.io.*;
import java.util.Enumeration;
import java.util.jar.*;
public class Decompression {
public Decompression() {}
public static void main(String args[]) throws FileNotFoundException, IOException{
File file=new File("c:\\test.jar");
JarFile jarFile=new JarFile(file);
//////////jar文件清单
Manifest manifest=jarFile.getManifest(); //返回jar文件清单,如果没有,则返回 null
Attributes attr=manifest.getMainAttributes(); //返回 Manifest 的主 Attributes
attr.putValue("decompression","解包打包测试"); //添加一个属性.
//////////
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream jarOutputStream = new JarOutputStream(baos,manifest);//使用指定的Manifest创建新的JarOutputStream
//////////zip 文件条目的枚举
Enumeration<JarEntry> jarEntrys=jarFile.entries();//返回 zip 文件条目的枚举
while(jarEntrys.hasMoreElements()){
JarEntry jarEntry=jarEntrys.nextElement();
if(!jarEntry.getName().endsWith("MANIFEST.MF")){ //因为在JarOutputStream构造函数中已经使用了Manifest,所以在此需要去掉META-INF/MANIFEST.MF
jarOutputStream.putNextEntry(jarEntry);
BufferedInputStream buffer=new BufferedInputStream(jarFile.getInputStream(jarEntry));
byte[] bfile=new byte[buffer.available()];
buffer.read(bfile);
jarOutputStream.write(bfile);
}
}
/////////
jarOutputStream.close();
/////////////将jar文件流写入到jar文件中
File fo=new File("c:\\newTest.jar");
FileOutputStream fout=new FileOutputStream(fo);
fout.write(baos.toByteArray()); //将jar文件字节流写入到newTest.jar文件中
fout.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -