java小程序(实现压缩和解压.zip格式文件)

java小程序(实现压缩和解压.zip格式文件)

2023年6月28日发(作者:)

JAVA小型项目开发报告

一、 项目简介

程序可以完成生成zip格式文件和解压zip格式文件操作,并且可以指定文件的路径

二、 开发环境

Windows10,JDK1.8。

开发软件:Eclipse+NetBeans

三、 业务流程图

四、 UML类图

五、 核心算法流程图

1

六、 程序代码

package Zipdemo;//程序主界面

import static ter;

import ion;

import t;

import ;

import nPane;

public class mainZipUl extends {

private ZipFileUl itsZipFileUl;

private UnZipFileUl itsUnZipFileUl;

public mainZipUl() {

initComponents();

le("Java解压小程序");

setcenter(this);

izable(false);

}

private void initComponents() {

jLabel1 = new ();

jLabel2 = new ();

2

UnZipFile = new n();

about = new n();

ZipFile = new n();

jLabel3 = new ();

setDefaultCloseOperation(_ON_CLOSE);

setBackground(new (211, 238, 238));

t("小程序可以实现压缩和解压文件功能");

t("选择你要的功能");

t("解压文件");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

UnZipFileActionPerformed(evt);

}

});

t("关于");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

aboutActionPerformed(evt);

}

});

t("压缩文件");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

ZipFileActionPerformed(evt);

}

});

t("欢迎来到Java解压小程序主界面");

ayout layout = new ayout(getContentPane());

getContentPane().setLayout(layout);

izontalGroup(ParallelGroup(G)

.addGroup(SequentialGroup()

.addGroup(ParallelGroup(G)

.addGroup(SequentialGroup().addGap(206, 206,

206)

.addComponent(UnZipFile,

RED_SIZE, 100,

RED_SIZE)

.addGap(56, 56, 56).addComponent(about,

RED_SIZE,

100,

RED_SIZE))

.addGroup(SequentialGroup().addGap(138, 138,

138).addComponent(

jLabel1, RED_SIZE,

198,

RED_SIZE))

.addGroup(SequentialGroup().addGap(181, 181,

181).addComponent(

jLabel2, RED_SIZE,

3 106,

RED_SIZE))

.addGroup(SequentialGroup().addGap(149, 149,

149).addComponent(jLabel3,

RED_SIZE, 177,

RED_SIZE)))

.addContainerGap(38, _VALUE))

.addGroup(ParallelGroup(G)

.addGroup(SequentialGroup().addGap(59, 59, 59)

.addComponent(ZipFile,

RED_SIZE, 100,

RED_SIZE)

.addContainerGap(341, _VALUE))));

ticalGroup(ParallelGroup(G)

.addGroup(SequentialGroup().addGap(66, 66, 66)

.addComponent(jLabel3, RED_SIZE,

36,

RED_SIZE)

.addPreferredGap(TED)

.addComponent(jLabel1, RED_SIZE,

38,

RED_SIZE)

.addGap(18, 18, 18)

.addComponent(jLabel2, RED_SIZE,

34,

RED_SIZE)

.addPreferredGap(D, 79,

_VALUE)

.addGroup(ParallelGroup(NE)

.addComponent(about,

RED_SIZE, 50,

RED_SIZE)

.addComponent(UnZipFile,

RED_SIZE, 50,

RED_SIZE))

.addGap(69, 69, 69))

.addGroup(ParallelGroup(G).addGroup(

NG,

SequentialGroup().addContainerGap(283,

_VALUE).addComponent(ZipFile,

RED_SIZE, 50,

RED_SIZE)

.addGap(67, 67, 67))));

pack();

}

private void aboutActionPerformed(Event evt) {

ssageDialog(null, "java小程序");

}

4 private void ZipFileActionPerformed(Event evt) {

ZipFileUl zfu = new ZipFileUl();

String name = (le() + "-" + t());

e();//释放此window机其组件

ible(true);

le(name);

}

private void UnZipFileActionPerformed(Event evt) {

UnZipFileUl uzf = new UnZipFileUl();

String name = (le() + "-" + t());

e();//释放此window机其组件

ible(true);

le(name);

}

public static void main(String args[]) {

try {

for (dFeelInfo info :

talledLookAndFeels()) {

if ("Nimbus".equals(e())) {

kAndFeel(ssName());

break;

}

}

} catch (ClassNotFoundException ex) {

ger(e()).log(, null, ex);

} catch (InstantiationException ex) {

ger(e()).log(, null, ex);

} catch (IllegalAccessException ex) {

ger(e()).log(, null, ex);

} catch (ortedLookAndFeelException ex) {

ger(e()).log(, null, ex);

}

Later(new Runnable() {

public void run() {

new mainZipUl().setVisible(true);

}

});

}

private n UnZipFile;

private n ZipFile;

private n about;

private jLabel1;

private jLabel2;

private jLabel3;

}

package Zipdemo;//压缩文件界面

5 import static ter;

import ;

import ;

import ;

import hooser;

import ;

import nPane;

public class ZipFileUl extends {

public Zipcode itsZipcode;

public ZipFileUl() {

initComponents();

setcenter(this);

izable(false);

table(false);

Dir_table(false);

}

private void initComponents() {

jLabel1 = new ();

Dir_name = new ield();

selectDir = new n();

jScrollPane1 = new lPane();

text1 = new rea();

exitBu = new n();

returnMU = new n();

Zipbu = new n();

setDefaultCloseOperation(_ON_CLOSE);

t("请选择要压缩的文件夹或文件");

t("选择要压缩的文件夹");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

selectDirActionPerformed(evt);

}

});

umns(20);

s(5);

wportView(text1);

t("退出程序");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

exitBuActionPerformed(evt);

}

});

t("返回主菜单");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

returnMUActionPerformed(evt);

}

});

t("压缩文件");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

ZipbuActionPerformed(evt);

}

});

ayout layout = new ayout(getContentPane());

getContentPane().setLayout(layout);

6 izontalGroup(ParallelGroup(G).addGroup(layout

.createSequentialGroup().addGap(40, 40, 40)

.addGroup(ParallelGroup(G).addGroup(layout

.createSequentialGroup()

.addGroup(ParallelGroup(G)

.addGroup(SequentialGroup().addGap(0, 6,

_VALUE)

.addComponent(Zipbu,

RED_SIZE, 99,

RED_SIZE)

.addGap(76, 76,

76).addComponent(returnMU).addGap(56, 56, 56)

.addComponent(exitBu,

RED_SIZE, 93,

RED_SIZE))

.addComponent(jScrollPane1))

.addGap(43, 43, 43))

.addGroup(NG,

SequentialGroup().addGap(0, 0,

_VALUE)

.addGroup(ParallelGroup(G)

.addComponent(Dir_name,

RED_SIZE, 276,

RED_SIZE)

.addComponent(jLabel1))

.addGap(18, 18,

18).addComponent(selectDir).addGap(21, 21, 21)))));

ticalGroup(ParallelGroup(G).addGroup(layout

.createSequentialGroup().addGap(20, 20, 20)

.addComponent(

jLabel1, RED_SIZE, 25,

RED_SIZE)

.addGap(18, 18, 18)

.addGroup(ParallelGroup(NE)

.addComponent(Dir_name,

RED_SIZE, 32,

RED_SIZE)

.addComponent(selectDir, T_SIZE, 32,

_VALUE))

.addGap(18, 18, 18)

.addGroup(ParallelGroup(NE)

.addComponent(Zipbu, RED_SIZE,

44,

RED_SIZE)

.addComponent(returnMU,

RED_SIZE, 44,

7 RED_SIZE)

.addComponent(exitBu, RED_SIZE,

44,

RED_SIZE))

.addPreferredGap(D, 27,

_VALUE)

.addComponent(jScrollPane1, RED_SIZE, 210,

RED_SIZE)

.addContainerGap()));

pack();

}

private void exitBuActionPerformed(Event evt) {

(0);

}

private void returnMUActionPerformed(Event evt) {

mainZipUl mu = new mainZipUl();

e();//释放当前窗口及其组件占用的资源

ible(true);

}

private void selectDirActionPerformed(Event evt) {

if (t().equals("选择要压缩的文件夹")) {

JFileChooser jfc = new JFileChooser();

eSelectionMode(ORIES_ONLY);

alog(new JLabel(), "选此文件夹");

File file = ectedFile();

Dir_t(olutePath());

Ssrc = olutePath();

t("保存到");

}

if (t().equals("保存到")) {

JFileChooser jfc = new JFileChooser();

eSelectionMode(ORIES_ONLY);

alog(new JLabel(), "保存到");

File file = ectedFile();

Dir_t(olutePath());

Dsrc = olutePath();

t("选择要压缩的文件夹");

}

}

private void ZipbuActionPerformed(Event evt) {

Zipcode user = new Zipcode();

String src = ;

String dest = ;

File srcf = new File(src);

File destf = new File(dest);

if (!()) {

ssageDialog(this, "文件或文件夹不存在,请检查后重试");

return;

}

if (!()) {

();

}

try {

String message = "压缩成功,文件存放在:" + dest + "目录下";

ssageDialog(this, message);

8 (src, dest, text1);

} catch (Exception ex) {

ger(e()).log(, null, ex);

}

}

private String Ssrc;//文件选择器获取的要压缩的文件路径

private String Dsrc;//文件选择器获取的目标地址

private ield Dir_name;

private n Zipbu;

private n exitBu;

private jLabel1;

private lPane jScrollPane1;

private n returnMU;

private n selectDir;

private rea text1;

public Zipcode getItsZipcode() {

return code;

}

}

package Zipdemo;//解压文件界面

import static ter;

import ;

import ption;

import ;

import ;

import hooser;

import ;

import nPane;

public class UnZipFileUl extends {

public Zipcode itsZipcode;

public UnZipFileUl() {

initComponents();

setcenter(this);

izable(false);

table(false);

table(false);

}

private void initComponents() {

jLabel1 = new ();

UnZipText = new ield();

selectZipFile = new n();

returnMBu = new n();

UnzipBu = new n();

exitBu2 = new n();

jScrollPane1 = new lPane();

text2 = new rea();

setDefaultCloseOperation(_ON_CLOSE);

t(" 请选择要解压的压缩文件");

t("打开压缩文件");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

selectZipFileActionPerformed(evt);

}

});

t("返回主菜单");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

9 returnMBuActionPerformed(evt);

}

});

t("解压");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

UnzipBuActionPerformed(evt);

}

});

t("退出程序");

ionListener(new Listener() {

public void actionPerformed(Event evt) {

exitBu2ActionPerformed(evt);

}

});

umns(20);

s(5);

wportView(text2);

ayout layout = new ayout(getContentPane());

getContentPane().setLayout(layout);

izontalGroup(ParallelGroup(G)

.addGroup(SequentialGroup()

.addGroup(ParallelGroup(G)

.addGroup(SequentialGroup().addGap(45, 45, 45)

.addComponent(jLabel1,

RED_SIZE, 414,

RED_SIZE)

.addGap(0, 0, _VALUE))

.addGroup(SequentialGroup().addGap(25, 25, 25)

.addGroup(ParallelGroup(G)

.addComponent(jScrollPane1,

NG)

.addGroup(SequentialGroup()

.addComponent(UnzipBu,

RED_SIZE,

139,

RED_SIZE)

.addGap(18, 18, 18)

.addComponent(returnMBu,

RED_SIZE,

139,

RED_SIZE)

.addPreferredGap(

D,

T_SIZE, _VALUE)

.addComponent(exitBu2,

RED_SIZE,

139,

RED_SIZE))

.addGroup(SequentialGroup()

.addComponent(UnZipText,

10 RED_SIZE,

321,

RED_SIZE)

.addGap(32, 32,

32).addComponent(selectZipFile,

T_SIZE, 112,

_VALUE)))))

.addContainerGap()));

ticalGroup(ParallelGroup(G)

.addGroup(SequentialGroup().addGap(25, 25, 25)

.addComponent(jLabel1, RED_SIZE,

35,

RED_SIZE)

.addPreferredGap(TED)

.addGroup(ParallelGroup(G, false)

.addComponent(UnZipText)

.addComponent(selectZipFile,

T_SIZE, 35, _VALUE))

.addGap(18, 18, 18)

.addGroup(ParallelGroup(NE)

.addComponent(returnMBu,

RED_SIZE, 48,

RED_SIZE)

.addComponent(exitBu2,

RED_SIZE, 48,

RED_SIZE)

.addComponent(UnzipBu,

RED_SIZE, 48,

RED_SIZE))

.addGap(18, 18, 18)

.addComponent(jScrollPane1, T_SIZE,

201, _VALUE)

.addContainerGap()));

pack();

}

private void exitBu2ActionPerformed(Event evt) {

(0);

}

private void returnMBuActionPerformed(Event evt) {

mainZipUl mu = new mainZipUl();

e();//释放此window机其组件

ible(true);

}

private void selectZipFileActionPerformed(Event evt) {

if (t().equals("打开压缩文件"))

{

JFileChooser jfc = new JFileChooser();

eSelectionMode(_ONLY);

alog(new JLabel(), "确定");

File file = ectedFile();

t(olutePath());

Src_zip = olutePath();

11 t("解压到");

}

if (t().equals("解压到")) {

JFileChooser jfc = new JFileChooser();

eSelectionMode(ORIES_ONLY);

alog(new JLabel(), "确定");

File file = ectedFile();

t(olutePath());

Dest_dir = olutePath();

t("打开压缩文件");

}

}

private void UnzipBuActionPerformed(Event evt)

{//GEN-FIRST:event_UnzipBuActionPerformed

Zipcode user = new Zipcode();

String src_file, dest_dir;

src_file = _zip;

dest_dir = _dir;

if (src_("")) {

ssageDialog(this, "未输入或选择解压文件,请检查后重试");

return;

}

try {

(src_file, dest_dir, text2);

String message = "解压成功,文件存放在:" + dest_dir + "目录下";

ssageDialog(this, message);

} catch (Exception ex) {

ger(e()).log(, null, ex);

}

}

private String Src_zip = "";

private String Dest_dir = "";

private ield UnZipText;

private n UnzipBu;

private n exitBu2;

private jLabel1;

private lPane jScrollPane1;

private n returnMBu;

private n selectZipFile;

private rea text2;

public Zipcode getItsZipcode() {

return code;

}

}

package Zipdemo;//压缩和解压功能的实现

import ion;

import t;

import ;

import eUl;

import ileUl;

import .*;

import rdCharsets;

import ;

import ;

import .*;

import nPane;

12 import rea;

public class Zipcode {

public static void setcenter(JFrame f) {

Toolkit tk = aultToolkit();

Dimension d = eenSize();

int screenWidth = (int) (th());

int screenHeight = (int) ght();

int frameWidth = th();

int frameHeight = th();

nds((screenWidth - frameWidth) / 2, (screenHeight - frameHeight) / 2, frameWidth,

frameHeight);

}

public void zip(String src_name, String dest_name, JTextArea jt) throws Exception {

File srcfile = new File(src_name);

String zfname = dest_name + tor + e() + ".zip";

FileOutputStream zipFile = new FileOutputStream(zfname);

("压缩中...n");

ZipOutputStream zout = new ZipOutputStream(zipFile, _8);

BufferedOutputStream bo = new BufferedOutputStream(zout);

zip(zout, srcfile, e(), bo, jt);

();

(); // 输出流关闭

("压缩完成n");

}

public void zip(ZipOutputStream zout, File f, String src, BufferedOutputStream bo, JTextArea jt)

throws Exception {

if (ctory()) {

File[] fl = les();

if ( == 0) {

tEntry(new ZipEntry(src + tor)); // 创建zip压缩进入点base

("正在压缩:" + src + tor + "n");

}

for (int i = 0; i < ; i++) {

zip(zout, fl[i], src + tor + fl[i].getName(), bo, jt); // 递归遍历子文件夹

}

} else {

tEntry(new ZipEntry(src)); // 创建zip压缩进入点base

("正在压缩:" + src + "n");

FileInputStream in = new FileInputStream(f);

BufferedInputStream bi = new BufferedInputStream(in);

int b;

while ((b = ()) != -1) {

(b); // 将字节流写入当前zip目录

();

}

();

(); // 输入流关闭

}

}

public void unzip(String ZipFile, String dest_dir, JTextArea jt) {

File file = new File(ZipFile);//当前压缩文件

ZipInputStream zin;//创建ZipInputStream对象

try {

ZipFile zipFile = new ZipFile(file);//创建压缩文件对象

zin = new ZipInputStream(new FileInputStream(file));//实例化对象,指明要解压的文件

ZipEntry entry;

13 while (((entry = tEntry()) != null) && !ctory()) {//如果entry不为空,并不在同一个目录下

File tmp = new File(dest_dir + tor + e());//解压出的文件路径

if (!()) {//如果文件不存在

entFile().mkdirs();//创建文件父类文件夹路径

OutputStream os = new FileOutputStream(tmp);//将文件目录中的文件放入输出流

//用输入流读取压缩文件中中的文件

InputStream in = utStream(entry);

int count = 0;

while ((count = ()) != -1) {//如有输入流可以读取到数值

(count);//输出流写入

}

();

();

}

ntry();

(e() + "解压成功n");

}

();

} catch (IOException e) {

tackTrace();

}

}

}

七、 运行结果

主界面:

14 压缩功能界面:

15

压缩功能测试:

16

解压功能测试:

17 八、 项目小结

程序能够解压和压缩小文件。对于比较大的文件,解压耗时会比较长或者解压失败,程序会出现无响应状态。在解压大文件方面有待于改进。

18

发布者:admin,转转请注明出处:http://www.yc00.com/news/1687895531a54764.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信