Я хотел написать программу GUI zip / unzip с Java. Программа сможет сжимать любую комбинацию файлов и каталогов и распаковывать один или несколько сжатых файлов.
Теперь я только что закончил с функцией GUI и zip. Но функция zip, похоже, не работает должным образом, создавая файлы zip, которые каким-то образом повреждены. Я не мог найти, в чем именно проблема. Вроде с функцией compress
или функцией zipFile
.
когда я тестировал программу, результат был следующий:
192-168-1-101: tools user0 $ unzip test1.zip
Архив: test1.zip Подпись конца центрального каталога не найдена. Либо этот файл не является zip-файлом, либо он представляет собой один диск в составе архива, состоящего из нескольких частей. В последнем случае центральный каталог и комментарий к zip-файлу будут найдены на последнем диске (ах) этого архива.
unzip: не удается найти каталог zip-файлов в одном из test1.zip или test1.zip.zip, и не удается найти test1.zip.ZIP, точка.
Большое спасибо за вашу помощь.
Код следующий:
package javazip;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.util.zip.*;
public class JZip {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable () {
public void run () {
new JZipFrame();
}
});
}
}
class JZipFrame extends JFrame {
private JTextArea displayArea;
public JZipFrame () {
setTitle("JZip");
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth / 4, screenHeight / 4);
setLocation(screenWidth / 4, screenHeight / 4);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar menuBar = createMenuBar();
JScrollPane displayPanel = createDisplayPanel();
add(displayPanel);
setJMenuBar(menuBar);
setVisible(true);
}
private JMenuBar createMenuBar () {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Operations");
JMenuItem compressItem = createCompressItem();
JMenuItem decompressItem = createDecompressItem();
JMenuItem quitItem = createQuitItem();
menu.add(compressItem);
menu.add(decompressItem);
menu.add(quitItem);
menuBar.add(menu);
return menuBar;
}
private JScrollPane createDisplayPanel () {
displayArea = new JTextArea(20, 40);
displayArea.setMargin(new Insets(5, 5, 5, 5));
displayArea.setEditable(false);
JScrollPane scrlPanel = new JScrollPane(displayArea);
return scrlPanel;
}
private JMenuItem createCompressItem () {
JMenuItem cItem = new JMenuItem("Compression");
cItem.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent event) {
File[] cChosenItems = selectFilesForCompression();
compress(cChosenItems);
}
});
return cItem;
}
private JMenuItem createDecompressItem () {
JMenuItem dItem = new JMenuItem("Decompression");
dItem.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent event) {
File[] dChosenItems = selectFilesForDecompression();
decompress(dChosenItems);
}
});
return dItem;
}
private final File[] selectFilesForCompression () {
final JFileChooser cChooser = new JFileChooser();
File[] selectedItems = null;
cChooser.setMultiSelectionEnabled(true);
cChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int cRetVal = cChooser.showOpenDialog(JZipFrame.this);
if (cRetVal == JFileChooser.APPROVE_OPTION) {
selectedItems = cChooser.getSelectedFiles();
for (File item : selectedItems)
displayArea.append(item.getParent() + System.getProperty("file.separator")
+ item.getName() + "\n");
}
return selectedItems;
}
private final File[] selectFilesForDecompression () {
final JFileChooser dChooser = new JFileChooser();
dChooser.setMultiSelectionEnabled(true);
dChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
dChooser.addChoosableFileFilter(new FileFilter() {
public String getDescription () {
return "ZIP Files (*.zip)";
}
public boolean accept (File f) {
if (f.isDirectory())
return true;
else
return f.getName().toLowerCase().endsWith(".zip");
}
});
int dRetVal = dChooser.showOpenDialog(JZipFrame.this);
File[] selectedItems = null;
if (dRetVal == JFileChooser.APPROVE_OPTION) {
selectedItems = dChooser.getSelectedFiles();
for (File item : selectedItems)
displayArea.append(item.getParent() + System.getProperty("file.separator")
+ item.getName() + "\n");
}
return selectedItems;
}
private JMenuItem createQuitItem () {
JMenuItem qItem = new JMenuItem("Quit");
qItem.addActionListener(new ActionListener () {
@Override
public void actionPerformed (ActionEvent event) {
System.exit(0);
}
});
return qItem;
}
private final void compress (File[] files) {
FileOutputStream out = null;
String zipName = null;
if (files.length == 1) {
zipName = files[0].getName();
if (files[0].isDirectory()) {
if (zipName.endsWith("/") || zipName.endsWith("\\"))
zipName = zipName.substring(0, zipName.length() - 1);
}
try {
String name = zipName.substring(0, zipName.lastIndexOf("."));
out = new FileOutputStream(name + ".zip");
}
catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
else {
try {
out = new FileOutputStream(files[0].getParent() + "/compressed.zip");
}
catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
ZipOutputStream zipOut = new ZipOutputStream(out);
zipOut.setMethod(ZipOutputStream.DEFLATED);
for (File f : files) {
try {
zipFile(f, f.getName(), zipOut);
}
catch (IOException e) {
JOptionPane.showMessageDialog(this, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
displayArea.append("Now processing: " + f.getName() + "\n");
}
JOptionPane.showMessageDialog(this, "Compression was successful!", "Message", JOptionPane.INFORMATION_MESSAGE);
}
// The problem may be with this function or the one above
private void zipFile (File file, String fileName, ZipOutputStream zipOut) throws IOException {
if (file.isHidden()) {
return;
}
if (file.isDirectory()) {
if (fileName.endsWith("/") || fileName.endsWith("\\")) {
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
}
else {
zipOut.putNextEntry(new ZipEntry(fileName + "/"));
zipOut.closeEntry();
}
File[] children = file.listFiles();
for (File childFile : children)
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
return;
}
FileInputStream input = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = input.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
input.close();
}
private final void decompress (File[] files) {
// TODO
}
}