nifest ZipSelfExtractor.class
7.执行java -jar myzip.jar就可以看到效果了,试试看
后记
一个自解压的jar文件能够很好的跨平台使用,自解压jar文件创建简单,只需要有jre1.2或或者更新的版本就可以实现了。
附自解压程序的源代码:
/* ZipSelfExtractor.java */
/* Author: Z.S. Jin
Updates: John D. Mitchell */
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.util.zip.*;
import java.util.*;
import java.text.*;
public class ZipSelfExtractor extends JFrame
{
private String myClassName;
static String MANIFEST = "META-INF/MANIFEST.MF";
public static void main(String[] args)
{
ZipSelfExtractor zse = new ZipSelfExtractor();
String jarFileName = zse.getJarFileName();
zse.extract(jarFileName);
System.exit(0);
}
ZipSelfExtractor()
{
}
private String getJarFileName()
{
myClassName = this.getClass().getName() + ".class";
URL urlJar = this.getClass().getClassLoader().getSystemResource(myClassName);
String urlStr = urlJar.toString();
int from = "jar:file:".length();
int to = urlStr.indexOf("!/");
return urlStr.substring(from, to);
}
public void extract(String zipfile)
{
File currentArchive = new File(zipfile);
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
fc.setDialogType(JFileChooser.OPEN_DIALOG);
fc.setDialogTitle("Select destination directory for extracting " +
currentArchive.getName());
fc.setMultiSelectionEnabled(false);
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showDialog(ZipSelfExtractor.this, "Select")
!= JFileChooser.APPROVE_OPTION)
{
return; //only when user select valid dir, it can return approve_option
}
File outputDir = fc.getSelectedFile();
byte[] buf = new byte[1024];
SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy hh:mma",Locale.getDefault());
ProgressMonitor pm = null;
boolean overwrite = false;
ZipFile zf = null;
FileOutputStream out = null;
InputStream in = null;
try
{
zf = new ZipFile(currentArchive);
int size = zf.size();
int extracted = 0;
pm = new ProgressMonitor(getParent(), "Extracting files...", "starting", 0, size-4);
pm.setMillisToDecideToPopup(0);
pm.setMillisToPopup(0);
Enumeration entries = zf.entries();
for (int i=0; i<size; i++)
{
ZipEntry entry = (ZipEntry) entries.nextElement();
if(entry.isDirectory())
continue;
String pathname = entry.getName();
if(myClassName.equals(pathname) || MANIFEST.equals(pathname.toUpperCase()))
continue;
extracted ++;
pm.setProgress(i);
pm.setNote(pathname);
if(pm.isCanceled())
return;
in = zf.getInputStream(entry);
File outFile = new File(outputDir, pathname);
Date archiveTime = new Date(entry.getTime());
if(overwrite==false)
{
if(outFile.exists())
{
Object[] options = {"Yes", "Yes To All", "No&q
|