软件截图如上图,首先选择一个文件,输入压缩后的文件名
点击解压:
选择压缩好的文件进行解压:
完整代码如下:
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
public class yasuotools {
private JFrame frame;
private JTextField textField;
public String path;
public String path2;
public String path1;
public String path3;
public String name;
private JTextField textField_1;
private static File file;
private final static int BUFFER_SIZE = 20480;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
yasuotools window = new yasuotools();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public yasuotools() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("\u538B\u7F29\u89E3\u538B\u5DE5\u5177");
frame.setBounds(100, 100, 647, 310);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("\u8BF7\u9009\u62E9\u4E00\u4E2A\u6587\u4EF6\uFF1A");
label.setFont(new Font("隶书", Font.PLAIN, 20));
textField = new JTextField();
textField.setFont(new Font("隶书", Font.PLAIN, 20));
textField.setColumns(10);
JButton btnNewButton = new JButton("\u9009\u62E9\u6587\u4EF6");
btnNewButton.setFont(new Font("隶书", Font.PLAIN, 20));
JButton button = new JButton("\u538B\u7F29");
button.setFont(new Font("隶书", Font.PLAIN, 20));
JButton button_1 = new JButton("\u89E3\u538B");
button_1.setFont(new Font("隶书", Font.PLAIN, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser filechooser = new JFileChooser();
filechooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置仅能选择文件
filechooser.setMultiSelectionEnabled(true);//
int returnValue = filechooser.showOpenDialog(filechooser);
{
if(returnValue == JFileChooser.APPROVE_OPTION)
{
file = filechooser.getSelectedFile();
path=file.getAbsolutePath();
name=file.getName();
textField.setText(path);
path1=file.getParent();
}
}
}
});
JLabel label_1 = new JLabel("\u538B\u7F29\u540E\u7684\u6587\u4EF6\u540D\uFF1A");
label_1.setFont(new Font("隶书", Font.PLAIN, 20));
textField_1 = new JTextField();
textField_1.setFont(new Font("隶书", Font.PLAIN, 20));
textField_1.setColumns(10);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
zipOut(file, path1+textField_1.getText()+".zip");
JOptionPane.showMessageDialog(null, "压缩成功啦!!╮(╯▽╰)╭", "恭喜:",JOptionPane.PLAIN_MESSAGE);
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "压缩失败了!(;′⌒`)", "错误",JOptionPane.ERROR_MESSAGE);
}
}
});
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
unZip(path);
JOptionPane.showMessageDialog(null, "解压成功啦!!╮(╯▽╰)╭", "恭喜:",JOptionPane.PLAIN_MESSAGE);
}
});
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(54)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 229, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(label)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 229, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(45, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(127)
.addComponent(button, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 134, Short.MAX_VALUE)
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
.addGap(126))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(63)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(btnNewButton))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
.addGap(79))
);
frame.getContentPane().setLayout(groupLayout);
}
private static void zipOut(File file, String dest) throws IOException {
// 创建文件输入流 和 zip输出流
FileInputStream fis = null;
ZipOutputStream zos = new ZipOutputStream(
new FileOutputStream(dest));
// 获取文件夹内所以File对象,并将各文件逐一压缩
File[] files = file.listFiles();
for (File f : files) {
fis = new FileInputStream(f);// 读入单个文件
// 为每个文件添加zip条目
zos.putNextEntry(new ZipEntry(f.getName()));
// 将各文件压缩
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
}
zos.close();
if (fis != null)
fis.close();
}
public static void unZip(String path)
{
int count = -1;
String savepath = "";
File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
savepath = path.substring(0, path.lastIndexOf(".")) + File.separator; //保存解压文件目录
new File(savepath).mkdir(); //创建保存目录
ZipFile zipFile = null;
try
{
zipFile = new ZipFile(path); //解决中文乱码问题
Enumeration> entries = zipFile.entries();
while(entries.hasMoreElements())
{
byte buf[] = new byte[BUFFER_SIZE];
ZipEntry entry = (ZipEntry)entries.nextElement();
String filename = entry.getName();
boolean ismkdir = false;
if(filename.lastIndexOf("/") != -1){ //检查此文件是否带有文件夹
ismkdir = true;
}
filename = savepath + filename;
if(entry.isDirectory()){ //如果是文件夹先创建
file = new File(filename);
file.mkdirs();
continue;
}
file = new File(filename);
if(!file.exists()){ //如果是目录先创建
if(ismkdir){
new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs(); //目录先创建
}
}
file.createNewFile(); //创建文件
is = zipFile.getInputStream(entry);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, BUFFER_SIZE);
while((count = is.read(buf)) > -1)
{
bos.write(buf, 0, count);
}
bos.flush();
bos.close();
fos.close();
is.close();
}
zipFile.close();
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
try{
if(bos != null){
bos.close();
}
if(fos != null) {
fos.close();
}
if(is != null){
is.close();
}
if(zipFile != null){
zipFile.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
}