展开全部
这是我以前写的一个,你看看,应该和这个差不多。。32313133353236313431303231363533e78988e69d8331333262356132。。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.swing.JFrame;
public class TestZip extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
String doc;
long doczipsize = 0;
TextArea ta;
TextField infotip;
public void init() {
setLocation(200,150);
setTitle("文件压缩器");
setSize(600,450);
BorderLayout b = new BorderLayout();
setLayout(b);
MenuBar menubar = new MenuBar();
setMenuBar(menubar);
Menu fileMenu = new Menu("File",true);
Menu aboutMenu = new Menu("About",true);
MenuItem mi1 = new MenuItem("打开");
MenuItem mi2 = new MenuItem("压缩");
MenuItem mi3 = new MenuItem("解压");
MenuItem mi4 = new MenuItem("退出");
MenuItem mi0 = new MenuItem("关于");
fileMenu.add(mi1);
fileMenu.add(mi2);
fileMenu.add(mi3);
fileMenu.add(mi4);
aboutMenu.add(mi0);
menubar.add(fileMenu);
menubar.add(aboutMenu);
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
mi0.addActionListener(this);
ta = new TextArea();
add(BorderLayout.CENTER,ta);
infotip = new TextField();
add(BorderLayout.SOUTH,infotip);
setVisible(true);
}
private void newFile() {
FileDialog dlg = new FileDialog(this,"打开",FileDialog.LOAD);
dlg.setVisible(true);
String filename = dlg.getDirectory() + dlg.getFile();
try {
File f = new File(filename);
FileReader freader = new FileReader(f);
char[] buffer = new char[4096];
int length;
ta.setText("");
while((length = freader.read(buffer)) != -1) {
String s = new String(buffer,0,length);
doc += s;
}
freader.close();
doczipsize = f.length();
showTextInfo();
}catch (IOException e) {
e.printStackTrace();
}
}
private void openFile() {
FileDialog dlg = new FileDialog(this,"解压",FileDialog.LOAD);
dlg.setVisible(true);
String filename = dlg.getDirectory() + dlg.getFile();
try {
File ff = new File(filename);
if(!ff.exists()) {
return;
}
FileInputStream fileis = new FileInputStream(ff);
ZipInputStream zipis = new ZipInputStream(fileis);
zipis.getNextEntry();
DataInputStream dis = new DataInputStream(zipis);
doc = dis.readUTF();
dis.close();
doczipsize = ff.length();
showTextInfo();
}catch (IOException e) {
e.printStackTrace();
}
}
private void saveFile() {
FileDialog dlg = new FileDialog(this,"压缩",FileDialog.LOAD);
dlg.setVisible(true);
String filename = dlg.getDirectory() + dlg.getFile();
try {
if(!filename.endsWith(".zip")) {
filename += ".zip";
}
File f = new File(filename);
if(!f.exists()) {
f.createNewFile();
}
FileOutputStream fileos = new FileOutputStream(f);
ZipOutputStream zipos = new ZipOutputStream(fileos);
zipos.setMethod(ZipOutputStream.DEFLATED);
zipos.putNextEntry(new ZipEntry("zip"));
DataOutputStream dos = new DataOutputStream(zipos);
/*
doc = "";
doc = ta.getText();
*/
dos.writeUTF(doc);
dos.close();
doczipsize = f.length();
showTextInfo();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestZip zip = new TestZip();
zip.init();
}
private void showTextInfo() {
ta.replaceRange(doc, 0, ta.getText().length());
infotip.setText("uncompressed size:" + doc.length() + " compressed size:" + doczipsize);
}
public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand();
if("打开".equals(arg)) {
newFile();
} else if("压缩".equals(arg)) {
saveFile();
} else if("解压".equals(arg)) {
openFile();
} else if("退出".equals(arg)) {
System.exit(0);
} else if("关于".equals(arg)) {
}
}
}
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起