该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipFile;
public class ZipMain
{
private static JTextField targetFile;
private static String fileName;
public static void main(String args[])
{
JFrame mainF = new JFrame();
mainF.setSize(new Dimension(300, 100));
mainF.setLocation(200, 100);
mainF.setTitle("压缩工具");
mainF.setLayout(new BorderLayout());
JPanel mainP = new JPanel(new GridLayout(2, 2));// 主界面
mainP.setSize(new Dimension(300, 100));
mainF.add(mainP, BorderLayout.CENTER);
targetFile = new JTextField();// 文件地址栏
targetFile.setSize(100, 25);
mainP.add(targetFile);
JButton chooseFile = new JButton("浏览");
chooseFile.addActionListener(new ChooseFileListener());
mainP.add(chooseFile);
JButton zip = new JButton("压缩");
zip.addActionListener(new ZipListener());
mainP.add(zip);
JButton unzip = new JButton("解压");
unzip.addActionListener(new UnZipListener());
mainP.add(unzip);
mainF.setVisible(true);
}
static class ChooseFileListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(new JPanel()) == JFileChooser.APPROVE_OPTION)
{
fileName = chooser.getSelectedFile().getName();
targetFile
.setText(chooser.getSelectedFile().toURI().toString());
}
}
}
static class ZipListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
File file = new File(new URI(targetFile.getText()));
File zipFile = new File(new URI(targetFile.getText() + ".zip"));
InputStream input = new FileInputStream(file); // 定义文件的输入流
ZipOutputStream zipOut = null; // 声明压缩流对象
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName())); // 设置ZipEntry对象
zipOut.setComment("www.mldnjava.cn"); // 设置注释
int temp = 0;
while ((temp = input.read()) != -1)
{ // 读取内容
zipOut.write(temp); // 压缩输出
}
input.close(); // 关闭输入流
zipOut.close(); // 关闭输出流
} catch (URISyntaxException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
static class UnZipListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
File file;
try
{
file = new File(new URI(targetFile.getText()));
File outFile = null; // 输出文件的时候要有文件夹的操作
ZipFile zipFile = new ZipFile(file); // 实例化ZipFile对象
ZipInputStream zipInput = null; // 定义压缩输入流
OutputStream out = null; // 定义输出流,用于输出每一个实体内容
InputStream input = null; // 定义输入流,读取每一个ZipEntry
ZipEntry entry = null; // 每一个压缩实体
zipInput = new ZipInputStream(new FileInputStream(file)); // 实例化ZIpInputStream
while ((entry = zipInput.getNextEntry()) != null)
{ // 得到一个压缩实体
outFile = new File(new URI(targetFile.getText()+ entry.getName())); // 定义输出的文件路径
if (!outFile.getParentFile().exists())
{ // 如果输出文件夹不存在
outFile.getParentFile().mkdir(); // 创建文件夹
}
if (!outFile.exists())
{ // 判断输出文件是否存在
outFile.createNewFile(); // 创建文件
}
input = zipFile.getInputStream(entry); // 得到每一个实体的输入流
out = new FileOutputStream(outFile); // 实例化文件输出流
int temp = 0;
while ((temp = input.read()) != -1)
{
out.write(temp);
}
input.close(); // 关闭输入流
out.close(); // 关闭输出流
}
input.close();
} catch (URISyntaxException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ZipException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
} finally
{
}
}
}
}