程序设计题ZIP格式压缩解压系统设计
程序设计题:ZIP格式压缩/解压系统设计
【问题描述】 由于网络带宽的限制,开发以互联网为传输媒介的软件系统时,在运行过程中的数据传输效率会成为
一套软件系统性能的重要指标。由于网络的数据传输速度是软件运行的客观因素,因此,在这种情况下,程序设计人员首先考虑的减少软件系统运行过程中需要传输的数据量,如果有些数据必须要传输,则软件工程师通常将这些数据在发送端进行压缩,而在数据接收端将数据解压缩,从而主劢减少应用系统数据传输量。 JDK环境中提供了多种类型的数据压缩方式,总结起来,利用Java语言可以创建的数据文件压缩格式包括如下类型:
?ZIP格式
?GZIP格式
?JAR格式
通过设计,允许创建ZIP压缩文件,并对ZIP压缩文件中包含的文件进行显示、添加、解压、删除等操作。GUI界面与下图类似:
【实验目的】
要求学生能熟练使用基于Swing的GUI设计,熟练使用常用组件和容器,理解java事件处理机制,会查看API documentation完成设计仸务,熟练文件流的操作。
【基本功能】
,1, 通过菜单组件、按钮组件、文本框组件等完成创建ZIP压缩文件。 ,2, ZIP压缩文件中包含的文件进行显示、添加、解压、删除等操作 ,3, 功能的其他扩展
【指导建议】
完成实验
P97“Zip文件的读取与制作” 将程序进行修改,增加GUI设计,并完成基本功能, 其他功能扩展和完善。
【程序设计的开发环境】
JDK1.5 及JCreator350/400。
----------------------------------------------------------------------------------------------------------------------
package com.huangyilun.work;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; import java.io.*;
import java.util.zip.*;
public class Zip_B08010304 extends JFrame implements ActionListener {
JFileChooser fileChooser;
JMenuBar menuBar;
JMenu menu1;
JMenuItem itemA, itemB;
JButton button1, button2;
JTextArea text1 = new JTextArea(5, 30);
JTextArea text2 = new JTextArea(1, 100);
Container con;
Zip_B08010304(String s) {
super(s);
setSize(400, 500);
setLocation(120, 120);
setVisible(true);
fileChooser = new JFileChooser();
menuBar = new JMenuBar();
menu1 = new JMenu("文件");
menuBar.add(menu1);
setJMenuBar(menuBar);
itemA = new JMenuItem("解压");
itemB = new JMenuItem("压缩");
menu1.add(itemA);
menu1.add(itemB);
button1 = new JButton("压缩");
button2 = new JButton("解压");
button1.setBounds(10, 10, 100, 120);
button2.setBounds(120, 10, 100, 120);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container con = getContentPane();
FlowLayout flow = new FlowLayout();
con.validate();
validate();
con.add(button1);
con.add(button2);
flow.setHgap(30);
con.add(text1);
con.add(text2);
text1.setLineWrap(true);
text1.setWrapStyleWord(false);
con.setLayout(flow);
itemA.addActionListener(this);
itemB.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
File f;
byte b[] = new byte[100];
;
if (e.getSource() == button2) {
fileChooser.setMultiSelectionEnabled(false);
int n = fileChooser.showOpenDialog(con);
if (n == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(
file));
ZipEntry zipEntry = null;
while ((zipEntry = in.getNextEntry()) != null) {
File dir = new File(file.getName().substring(0,
file.getName().indexOf(".") + 1));
dir.mkdir();
File fil = new File(dir, zipEntry.getName());
FileOutputStream out = new FileOutputStream(fil);
int m = -1;
text1.setText("解压的文件为:" + file.getName() + "\n存在于目
录:"
+ file.getParent() + "\n解压后的文件大小为:"
+ ((file.length()) / 1024) + "KB");
while ((m = in.read(b, 0, 100)) != -1) {
out.write(b, 0, m);
}
out.close();
}
in.close();
} catch (IOException ee) {
System.out.println(ee);
}
}
} else if (e.getSource() == itemA) {
fileChooser.setMultiSelectionEnabled(false);
int n = fileChooser.showOpenDialog(con);
if (n == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(
file));
ZipEntry zipEntry = null;
while ((zipEntry = in.getNextEntry()) != null) {
File dir = new File(file.getName().substring(0,
file.getName().indexOf(".") + 1));
dir.mkdir();
File fil = new File(dir, zipEntry.getName());
FileOutputStream out = new FileOutputStream(fil);
int m = -1;
text1.setText("解压的文件为:" + file.getName() + "\n存在于目
录:"
+ file.getParent() + "\n解压后的文件大小为:"
+ ((file.length()) / 1024) + "KB");
while ((m = in.read(b, 0, 100)) != -1) {
out.write(b, 0, m);
}
out.close();
}
in.close();
} catch (IOException ee) {
System.out.println(ee);
}
}
} else if (e.getSource() == button1) {
fileChooser.setMultiSelectionEnabled(true);
int n = fileChooser.showOpenDialog(con);
if (n == JFileChooser.APPROVE_OPTION) {
f = new File("first.zip");
File file[] = fileChooser.getSelectedFiles();
try {
int i;
ZipOutputStream out = new ZipOutputStream(
new FileOutputStream(f));
for (i = 0; i < file.length; i++) {
out.putNextEntry(new ZipEntry(file[i].getName()));
FileInputStream reader = new FileInputStream(file[i]);
int m = -1;
text1.append("压缩的文件为:" + file[i].getName() + "\n存在于目录:"
+ file[i].getParent() + "\n文件的大小为:"
+ ((file[i].length()) / 1024) + "KB" + "\n");
while ((m = reader.read(b, 0, 100)) != -1) {
out.write(b, 0, m);
}
}
out.close();
text1.append("压缩后文件的大小为;" + ((f.length()) / 1024) + "KB");
} catch (IOException ee) {
System.out.println(ee);
}
}
} else if (e.getSource() == itemB) {
fileChooser.setMultiSelectionEnabled(true);
int n = fileChooser.showOpenDialog(con);
if (n == JFileChooser.APPROVE_OPTION) {
f = new File("first.zip");
File file[] = fileChooser.getSelectedFiles();
try {
int i;
ZipOutputStream out = new ZipOutputStream(
new FileOutputStream(f));
for (i = 0; i < file.length; i++) {
out.putNextEntry(new ZipEntry(file[i].getName()));
FileInputStream reader = new FileInputStream(file[i]);
int m = -1;
text1.append("压缩的文件为:" + file[i].getName() + "\n存在于目
录:"
+ file[i].getParent() + file[i].getParent()
+ "\n文件的大小为:" + ((file[i].length()) / 1024)
+ "KB" + "\n");
while ((m = reader.read(b, 0, 100)) != -1) {
out.write(b, 0, m);
}
}
out.close();
text1.append("压缩后文件的大小为:" + ((f.length()) / 1024) + "KB");
} catch (IOException ee) {
System.out.println(ee);
}
}
}
}
public static void main(String args[]) {
new Zip_B08010304("Zip_B08010304");
}
}