⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipfile.java~1~

📁 用于处理压缩的软件
💻 JAVA~1~
字号:
package yasuowenjian;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.event.*;import java.awt.event.WindowEvent;import java.util.zip.ZipInputStream;import javax.swing.JFileChooser;import javax.swing.UIManager;import javax.swing.*;import java.awt.FileDialog;import javax.swing.ProgressMonitorInputStream;public class ZipFile extends JFrame implements ActionListener,Runnable{int filesize2= 0;JFileChooser file;JProgressBar allfilebar= new JProgressBar(0,100);ProgressMonitorInputStream pm;Thread runner;int filesize=0;int totalsize, standered=0;;File filename;File[] choosefile;String lujing,wenjianming,firstpathto,pathto;FileInputStream fileinput,zipinput;BufferedInputStream bufferinput,bufferzip;ZipOutputStream zipout;ZipInputStream zipin;ZipEntry entry;Container pane, tipcontent;JTextArea tiparea=new JTextArea(20,40);JWindow tipwindow;boolean show,change=true,change2=true;String tip="欢迎使用!";int allfilesize,allFileSize=0;int currentsize;public ZipFile(){pane = getContentPane();pane.setLayout(new GridLayout(4,1));JButton choosebutton=new JButton("选择文件或文件夹");choosebutton.setBackground(Color.green );choosebutton .addActionListener(this);pane.add(choosebutton);JPanel frontpanel=new JPanel();frontpanel.setLayout(new GridLayout(2,2));JButton pressbutton=new JButton ("压缩");pressbutton.setBackground(Color.pink );pressbutton .addActionListener(this);frontpanel.add(pressbutton);pressbutton=new JButton ("压缩到");pressbutton.setBackground(Color.pink );pressbutton .addActionListener(this);frontpanel.add(pressbutton);pressbutton=new JButton ("解压");pressbutton.setBackground(Color.pink );pressbutton .addActionListener(this);frontpanel.add(pressbutton);pressbutton=new JButton ("解压到");pressbutton.setBackground(Color.pink );pressbutton .addActionListener(this);frontpanel.add(pressbutton);pane.add(frontpanel);JButton waitbutton =new JButton ("打开提示信息框");waitbutton .setBackground(Color.pink ) ;waitbutton .addActionListener(this);pane.add(waitbutton);allfilebar.setStringPainted(true);pane.add(allfilebar);setSize(200,200);addWindowListener(new WindowDestroyer());show=true;tipwindow=new JWindow();tipcontent=tipwindow.getContentPane();tipcontent.setLayout(new BorderLayout());tipcontent.setBackground(Color.green );tipcontent.add(tiparea,BorderLayout.CENTER );tipwindow.setSize(200,200);showtip(tip,show);}public void showtip(String tip,boolean show2){tipwindow.setLocation(getX()+200 ,getY() );tiparea.setBackground(Color.lightGray ) ;Font f=new Font("隶书",Font.BOLD ,20);tiparea.setFont(f);tiparea.setText(tip);tiparea.setLineWrap(true);tipwindow.setVisible(show2);}public void actionPerformed (ActionEvent e){String event=e.getActionCommand() ;if(event.equals("打开提示信息框") ){show=!show;showtip(tip,show);}else if(event.equals("选择文件或文件夹") ){choosefile();}else if(event.equals("压缩") ){runner=new Thread(this);tip="你正在进行的是压缩过程,请稍等。。。。。。";showtip(tip,show);try {standered=1;runner.start();runner=null  ;}catch (Exception ex1) {}}else if (event.equals("压缩到") ){runner=new Thread(this);choosepath();tip="你正在进行的是压缩到过程,请稍等。。。。。。";showtip(tip,show);change=false;standered=1;runner.start() ;runner=null;}else if (event.equals("解压") ){runner=new Thread(this);tip="你正在进行的是解压缩过程,请稍等。。。。。。";showtip(tip,show);standered=2;runner.start() ;runner=null;}else if (event.equals("解压到") ){choosepath();runner=new Thread(this);tip="你正在进行的是解压到过程,请稍等。。。。。。";showtip(tip,show);standered=2;change2=false;runner.start();    }}private void zip(ZipOutputStream out,File file,String entryName) throws Exception{if(file.isDirectory()){File[] fileLists=file.listFiles();out.putNextEntry(new ZipEntry(entryName+"/"));int length=fileLists.length;for(int i=0;i<length;i++){zip(out,fileLists[i],entryName+"/"+fileLists[i].getName());}}else{int BUFFER=256;byte data[] = new byte[BUFFER];out.putNextEntry(new ZipEntry(entryName));FileInputStream fi = new FileInputStream(entryName);BufferedInputStream or = new BufferedInputStream(fi, BUFFER);int c;while((c=fi.read(data,0,BUFFER)) != -1) {out.write(data);currentsize +=BUFFER;runner.sleep((long)0.1);allfilebar.setValue((int )currentsize*100/allfilesize);}}}public  void unzip(String zipFileName,String outputDirectory) {FileInputStream fis = null;try {  fis = new FileInputStream(zipFileName);}catch (FileNotFoundException ex) {}zipin= new ZipInputStream(new BufferedInputStream(fis));ZipEntry z;try {while ( (z = zipin.getNextEntry()) != null) {if (z.isDirectory()) {String name = z.getName();name = name.substring(0, name.length() - 1);File f = new File(outputDirectory + File.separator + getFileName(name));f.mkdir();}else {File f = new File(outputDirectory + File.separator +getFileName(z.getName()));f.createNewFile();int BUFFER = 2048;int count;byte data[] = new byte[BUFFER];FileOutputStream fos = new FileOutputStream(f);BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);while ( (count = zipin.read(data, 0, BUFFER)) != -1) {filesize2+=count;dest.write(data, 0, count);try {  runner.sleep( (long) 1);}catch (InterruptedException ex2) {}allfilebar.setValue((int )filesize2*100/allfilesize);}dest.close();}}System.out .print(filesize2) ;}catch (IOException ex1) {}}public String getFileName(String path){int length=path.length();String fileName="";boolean file=true;int fileBorder=0;for(int i=length-1;i>=0&&file;i--){if(path.charAt(i)=='\\'){file = false;fileBorder=i+1;}}for(int i=fileBorder;i<length;i++){fileName+=path.charAt(i);}return fileName;}public String setZipName(String fileName){int length=fileName.length();boolean post=true;String postName="";for(int i=0;i<length&&post;i++){if(fileName.charAt(i)=='.'){post=false;}if(post){ postName += fileName.charAt(i);}}postName+=".zip";return postName;}private int getAllSize(File[] file){for (int i=0;i<file.length ;i++){if(file[i].isDirectory()){File[] files=file[i].listFiles(); getAllSize(files);}else{allFileSize +=(int)file[i].length();}}return  allFileSize;}public void choosefile(){file = new JFileChooser();file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);file.setMultiSelectionEnabled(true);file.showOpenDialog(this);choosefile=file.getSelectedFiles() ;}public void choosepath(){JFileChooser file = new JFileChooser();file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);file.setMultiSelectionEnabled(false);file.showOpenDialog(this);File pathfile;pathfile=file.getSelectedFile() ;firstpathto=pathfile.getParent() ;pathto=firstpathto+firstpathto.charAt(2) ;}public static void main (String args[]){ZipFile mywork=new ZipFile();mywork.show();try {UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");}catch (UnsupportedLookAndFeelException ex) {}catch (IllegalAccessException ex) {}catch (InstantiationException ex) {}catch (ClassNotFoundException ex) {}}public void run(){if(choosefile==null)JOptionPane.showInputDialog("请先选择");else{allfilesize=getAllSize(choosefile);allfilebar.setValue(0);switch(standered){case 1:for(int i=0;i<choosefile.length;i++){try {System.out .print(choosefile.length ) ;     FileOutputStream dest;     if(change)dest = new  FileOutputStream(setZipName(choosefile[i].getPath() ));      else dest = new FileOutputStream(pathto + setZipName(choosefile[i].getName()));      zipout = new ZipOutputStream(new BufferedOutputStream(dest));     zip(zipout, choosefile[i], choosefile[i].getPath());}catch (Exception ex) {}}try {  zipout.close();}catch (IOException ex1) {}File newfile;int ziplength=0;for(int i=0;i<choosefile.length ;i++){if(change==false) newfile=new File(pathto+setZipName(choosefile[i].getName() )) ;else  newfile=new File(setZipName(choosefile[i].getPath() )) ;  ziplength +=(int)newfile.length();} tip="原文件大小为"+allfilesize+";"+ "压缩后的文件大小为 "+ziplength+";"+ "压缩比为"+(float)allfilesize/(float)ziplength+"."; showtip(tip,show); allfilesize=0; currentsize=0; allFileSize=0; break; case 2: FileInputStream fis = null; for(int i=0;i<choosefile.length ;i++){ try { fis = new FileInputStream(choosefile[i].getPath()); zipin = new ZipInputStream(new BufferedInputStream(fis)); } catch (FileNotFoundException ex3) {} if(change2) unzip(choosefile[i].getPath() ,choosefile[i].getParent()  ); else unzip(choosefile[i].getPath() ,firstpathto ); } tip="原文件大小为"+allfilesize+ "解压缩后的文件大小为"+filesize2; showtip(tip,show); allfilesize=0; filesize2=0; allFileSize=0; break;} }}private class WindowDestroyer extends WindowAdapter{public void windowClosing(WindowEvent e){System.exit(0) ;}}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -