📄 compressdialog.java
字号:
package zipCompress;
import java.io.*;
import java.util.zip.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class CompressDialog
extends JDialog {
//用于压缩的变量:
byte[] cache = new byte[1024 * 1024]; //1MB大小的缓冲区
FileInputStream fin;
FileOutputStream fout;
ZipEntry entry;
ZipOutputStream zout;
CompressFrame parent;
String defaultPath;
String defaultFileName;
boolean ifInput = false;
String pFrameDirectry;
File[] filesToCompress;
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JTextField jTextFeildPath = new JTextField();
JButton jButtonCd = new JButton();
JButton jButtonYes = new JButton();
JButton jButtonCancel = new JButton();
JScrollPane jScrollPane2 = new JScrollPane();
JTextArea jTextAreaProgress = new JTextArea();
public CompressDialog(Frame frame, String title, boolean modal) {
super(frame, title, modal);
parent = (CompressFrame) frame;
this.pFrameDirectry=parent.getCurrentDirectry();
try {
jbInit();
// pack();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public CompressDialog() {
this(null, "", false);
}
private void jbInit() throws Exception {
panel1.setLayout(null);
jLabel1.setFont(new java.awt.Font("新細明體", 0, 11));
jLabel1.setDebugGraphicsOptions(0);
jLabel1.setHorizontalAlignment(SwingConstants.LEADING);
jLabel1.setHorizontalTextPosition(SwingConstants.LEFT);
jLabel1.setIcon(new ImageIcon(CompressDialog.class.getResource("addTo.png")));
jLabel1.setIconTextGap(4);
jLabel1.setText("添加到:");
jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.CENTER);
jLabel1.setBounds(new Rectangle(19, 13, 89, 35));
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.
VERTICAL_SCROLLBAR_NEVER);
jScrollPane1.setBounds(new Rectangle(17, 57, 230, 39));
jTextFeildPath.setSelectionEnd(11);
jTextFeildPath.setText("");
jButtonCd.setBounds(new Rectangle(253, 57, 67, 25));
jButtonCd.setFont(new java.awt.Font("新細明體", 0, 11));
jButtonCd.setText("浏览...");
jButtonCd.addActionListener(new CompressDialog_jButtonCd_actionAdapter(this));
jButtonYes.setBounds(new Rectangle(69, 321, 71, 25));
jButtonYes.setFont(new java.awt.Font("新細明體", 0, 11));
jButtonYes.setForeground(Color.black);
jButtonYes.setVerifyInputWhenFocusTarget(true);
jButtonYes.setText("确定");
jButtonYes.addActionListener(new CompressDialog_jButtonYes_actionAdapter(this));
jButtonCancel.setBounds(new Rectangle(195, 321, 71, 25));
jButtonCancel.setFont(new java.awt.Font("新細明體", 0, 11));
jButtonCancel.setText("取消");
jButtonCancel.addActionListener(new CompressDialog_jButtonCancel_actionAdapter(this));
this.setModal(true);
this.setResizable(false);
jScrollPane2.setBounds(new Rectangle(20, 121, 301, 175));
jTextAreaProgress.setText("");
getContentPane().add(panel1);
panel1.add(jLabel1, null);
panel1.add(jScrollPane1, null);
panel1.add(jScrollPane2, null); panel1.add(jButtonCd, null); panel1.add(jButtonYes, null); panel1.add(jButtonCancel, null);
jScrollPane2.getViewport().add(jTextAreaProgress, null);
jScrollPane1.getViewport().add(jTextFeildPath, null);
this.initializeKeyValues(parent.getFilesSelected());
}
/*设置缺省值**/
private void initializeKeyValues(File[] files) {
this.defaultPath = files[0].getAbsolutePath();
if (!this.defaultPath.endsWith("\\")) {
this.defaultPath = this.defaultPath.substring(0,
this.defaultPath.
lastIndexOf('\\') + 1);
}
//以第一个文件名为缺省值:
String tempName = files[0].getName();
if (files[0].isFile()) {
tempName = tempName.substring(0, tempName.lastIndexOf('.'));
}
this.defaultFileName = tempName + ".zip";
this.jTextFeildPath.setText(this.defaultPath + this.defaultFileName);
}
/*******构建输出流**********/
private void constructStreams() {
try {
fout = new FileOutputStream(this.defaultPath + this.defaultFileName);
zout = new ZipOutputStream(fout);
zout.setMethod(ZipOutputStream.DEFLATED);
zout.setLevel(Deflater.DEFAULT_COMPRESSION);
}
catch (IOException ex) {
JOptionPane.showMessageDialog(this, "无法创建压缩文件", "压缩文件时出错",
JOptionPane.ERROR_MESSAGE);
this.dispose();
}
}
/*********检测用户输入***********/
private boolean validateInput() {
String userInput = this.jTextFeildPath.getText();
if (userInput.equals(this.defaultPath + this.defaultFileName)) { //没有输入
return true;
}
this.ifInput = true;
if (userInput == null || userInput.length() == 0) {
return false;
}
if (!userInput.endsWith(".zip")) {
return false;
}
try {
File f = new File(userInput);
f.createNewFile();
f.delete();
}
catch (IOException e) {
return false;
}
return true;
}
/*******获取合法输入**********/
private void getValidInput() {
if (!ifInput) {
return;
}
String userInput = this.jTextFeildPath.getText();
this.defaultPath = userInput.substring(0, userInput.lastIndexOf('\\'));
this.defaultFileName = userInput.substring(userInput.lastIndexOf('\\') + 1);
}
private void zipAll(File[] files) {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
zipAll(files[i].listFiles());
}
else {
this.zipOneFile(files[i]);
}
}
}
private void zipOneFile(File f) {
try {
fin = new FileInputStream(f);
entry = new ZipEntry(f.getAbsolutePath().substring(this.pFrameDirectry.length())); //用getPath()在压缩文件内部创建路径
entry.setTime(f.lastModified());
this.jTextAreaProgress.append("正在压缩文件:"+entry.getName());
zout.putNextEntry(entry);
int amount = fin.read(cache);
while (amount != -1) {
zout.write(cache, 0, amount);
amount = fin.read(cache);
}
//关闭fin
fin.close();
//写入结束标志
zout.closeEntry();
}
catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(this, "文件" + f.getName() + "不存在!",
"压缩文件时出错", JOptionPane.ERROR_MESSAGE);
}
catch (IOException ex1) {
JOptionPane.showMessageDialog(this, "无法创建压缩文件:文件拒绝访问", "压缩文件时出错",
JOptionPane.ERROR_MESSAGE);
}
}
void jButtonYes_actionPerformed(ActionEvent e) {
if (this.validateInput()) {
this.getValidInput();
}
else {
this.jTextFeildPath.setText(this.defaultPath + this.defaultFileName);
}
File testPath=new File(this.defaultPath);
if(!testPath.exists()){
testPath.mkdirs();
}
constructStreams();
zipAll(parent.getFilesSelected());
parent.reDisplay();
try {
this.zout.close();
}
catch (IOException ex) {
JOptionPane.showMessageDialog(this, "无法结束压缩文件:未知原因", "压缩文件时出错",
JOptionPane.ERROR_MESSAGE);
}
this.dispose();
}
void jButtonCancel_actionPerformed(ActionEvent e) {
this.dispose();
}
private void changeDirectory() {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(this.defaultPath));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setMultiSelectionEnabled(false);
chooser.showOpenDialog(this);
if (chooser.getSelectedFile() == null) {
return;
}
this.defaultPath = chooser.getSelectedFile().getAbsolutePath();
if(this.defaultPath.indexOf(".")!=-1){
this.defaultPath=this.defaultPath.substring(0,this.defaultPath.lastIndexOf("\\"));
}
if (!this.defaultPath.endsWith("\\")) {
this.defaultPath += "\\";
}
this.jTextFeildPath.setText(this.defaultPath + this.defaultFileName);
}
void jButtonCd_actionPerformed(ActionEvent e) {
this.changeDirectory();
}
}
class CompressDialog_jButtonYes_actionAdapter
implements java.awt.event.ActionListener {
CompressDialog adaptee;
CompressDialog_jButtonYes_actionAdapter(CompressDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButtonYes_actionPerformed(e);
}
}
class CompressDialog_jButtonCancel_actionAdapter
implements java.awt.event.ActionListener {
CompressDialog adaptee;
CompressDialog_jButtonCancel_actionAdapter(CompressDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButtonCancel_actionPerformed(e);
}
}
class CompressDialog_jButtonCd_actionAdapter
implements java.awt.event.ActionListener {
CompressDialog adaptee;
CompressDialog_jButtonCd_actionAdapter(CompressDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButtonCd_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -