用Java实现一个简单的GUI和文件压缩解压操作。最终效果如下。
实际上这个程序主要分为两个部分,分别是图形界面的部分和文件处理的部分。
图形界面:
用JTextField类实现文本的输入,通过继承ActionListener接口实现反馈操作
swing和awt是java中处理图形界面的类。Java的图形界面分为容器container和组件JComponent,容器上可以添加组建。
JPanel一般作为容器装其他组件,再添加到窗口上。
窗口一般从Jframe派生,对话框从Jdialog派生。具体的参数设定方法参见Java的类实现。
附一张图:
具体实现如下
扫描二维码关注公众号,回复:
1075010 查看本文章
//图形界面部分 package zip_program; import javax.swing.*; import java.awt.*; import javax.swing.border.*; import java.awt.event.*; import java.io.*; public class display extends JFrame implements ActionListener { JTextField text1,text2; JButton bt1, bt2; String str[] = {null, null}; File f[] = {null, null}; public display() { //Set FlowLayout,aligned left with horizontal gap 10 //and vertical gap 20 between components f[0] = f[1] = null; JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT,10,20)); //Add labels and text fields to the frame p1.add(new JLabel("路径: ")); text1 = new JTextField(30); p1.add(text1); Font largeFont = new Font("TimesRoman", Font.BOLD, 20); Border lineBorder = new LineBorder(Color.BLACK, 2); p1.add(new JLabel("文件名:")); text2 = new JTextField(30); p1.add(text2); JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(1,2,10,10)); //p2.add(null); text1.addActionListener(this); text2.addActionListener(this); text1.setText("请输入正确的绝对路径,回车键结束!"); text2.setText("请输入压缩文件的绝对路径,回车键结束!"); bt1 = new JButton("打包"); bt2 = new JButton("解包"); bt1.setFont(largeFont); bt2.setFont(largeFont); bt1.setBorder(lineBorder); bt2.setBorder(lineBorder); bt1.setBackground(Color.WHITE); bt2.setBackground(Color.WHITE); //bt1.setPreferredSize(new Dimension(1,2)); //bt1 是解包按钮,添加监视器调用压缩函数 bt1.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { boolean flag = true; if(f[0] == null || !f[0].exists()) { text1.setText("格式错误或路径不存在!请重新输入!"); flag = false; } if(f[1] == null) { text2.setText("格式错误或路径不存在!请重新输入!"); flag = false; } if(flag == true) { Zipfile.zip(f); } } }); //bt1 是解包按钮,调用解压函数 bt2.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { boolean flag = true; if(f[0] == null || !f[0].exists()) { text1.setText("格式错误或路径不存在!请重新输入!"); flag = false; } if(f[1] == null || !f[0].exists()) { text2.setText("待解压文件不存在!请重新输入!"); flag = false; } if(flag == true) { try { Zipfile.unZipFiles(f[1], str[0] + "/"); } catch (IOException e1) { } } } }); p2.add(bt1); p2.add(bt2); setLayout(new GridLayout(2,1,5,5)); add(p1); add(p2); } //添加文本监视器,并判断输入是否合法 public void actionPerformed(ActionEvent e) { if(e.getSource() == text1) { str[0] = text1.getText(); f[0] = new File(str[0]); if(f[0] == null || !f[0].exists()) { text1.setText("格式错误或路径不存在!请重新输入!"); } else { text1.setText("路径已输入!"); } } else { str[1] = text2.getText(); f[1] = new File(str[1]); if(!str[1].endsWith(".zip")) { text2.setText("请输入zip格式的文件路径!"); } else { text2.setText("路径已输入!"); } } } }
接下来是文件处理部分
感觉Java的文件处理流很多很复杂。很多东西我也没搞明白具体是怎么用的。大体上是把文件压缩至zip文件,如果有文件夹就递归的压缩。
package zip_program; import java.io.*; import java.nio.charset.Charset; import java.util.Enumeration; import java.util.zip.*; //文件解压与压缩 public final class Zipfile { //文件压缩函数,通过遍历子文件夹,递归的压缩文件 public static void zip(File[] f) { File source = f[0]; File target = f[1]; FileOutputStream fout = null; ZipOutputStream zout = null; try { fout = new FileOutputStream(target); zout = new ZipOutputStream(new BufferedOutputStream(fout)); addZip("../", source, zout); }catch(IOException e) { }finally { try { if(fout != null) { zout.close(); } if(zout != null) { fout.close(); } }catch(IOException e) { } } //输出压缩信息 System.out.println("******************压缩完毕********************"); System.out.println("文件已从"+f[0].getAbsolutePath()+"压缩至"+f[1].getAbsolutePath()); } //使用递归函数对文件进行压缩 public static void addZip(String base, File source, ZipOutputStream zout) throws IOException { String entry = base + source.getName(); if(source.isDirectory()) { for(File file : source.listFiles()) { addZip(entry + "/", file, zout); } } else { FileInputStream fin = null; BufferedInputStream bin = null; try { byte[] buffer = new byte[1024 * 10]; fin = new FileInputStream(source); bin = new BufferedInputStream(fin, buffer.length); int r = 0; zout.putNextEntry(new ZipEntry(entry)); while(((r = bin.read(buffer, 0, buffer.length)) != -1)) { zout.write(buffer, 0, r); } zout.closeEntry(); } finally { try { if(bin != null) { bin.close(); } if(fin != null) { fin.close(); } }catch(IOException e) { } } } } //解压文件 public static void unZipFiles(File zipFile,String descDir)throws IOException { File pathFile = new File(descDir); if(!pathFile.exists()) { pathFile.mkdirs(); } //保证中文可以被正确识别 ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK")); for(Enumeration entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry)entries.nextElement(); String zipEntryName = entry.getName(); InputStream in = zip.getInputStream(entry); String outPath = (descDir+zipEntryName).replaceAll("\\*", "/");; File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); if(!file.exists()) { file.mkdirs(); } if(new File(outPath).isDirectory()) { continue; } System.out.println(outPath); OutputStream out = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while((len=in.read(buf1))>0) { out.write(buf1,0,len); } in.close(); out.close(); } //输出解压信息 System.out.println("******************解压完毕********************"); System.out.println("文件已从"+zipFile.getAbsolutePath()+"解压至"+descDir.substring(0, descDir.length() - 1)); zip.close(); } }
zip主函数
package zip_program; import javax.swing.*; public class zip { public static void main(String[] args) { display frame = new display(); frame.setTitle("文件压缩程序"); frame.setSize(400,300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setResizable(false); } }