我想用Java编写一个GUI zip/unzip程序。该程序将能够压缩文件和目录/IES的任何组合,并解压缩一个或多个压缩文件。
现在我刚刚完成了GUI和zip Funtion。但是zip funtion似乎不能正常工作,产生的zip文件不知何故被破坏了。我找不到问题到底出在哪里。它似乎与compress
函数或zipfile
函数有关。
当我测试该程序时,输出如下:
归档:找不到test1.zip中央目录结束签名。要么这个文件不是zipfile,要么它构成了多部分归档的一个磁盘。在后一种情况下,中央目录和zipfile注释将在这个归档的最后一个磁盘上找到。
解压缩:在test1.zip或test1.zip.zip中找不到zipfile目录,也找不到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
}
}
在web上摸索之后,我找到了一个关于ZipoutStream
的教程,在其中我注意到ZipoutPutStream
对象在压缩操作后被关闭。因此,我添加了代码来关闭ZipoutputStream
对象,即在我的代码中的compress
方法中的zipout
。代码现在可以正确地压缩单个文件、多个文件、目录以及文件和目录/IES的组合。
因此,在使用输入流和输出流对象时,一定要记得在使用后关闭它们。否则缓冲区将无法正确地读取或写入以产生所需的结果。
现在,我将继续编写decompress
方法,并可能添加更多特性。我可能还会遇到问题,到那时我会来这里寻求帮助。这里的Peeple很有帮助,解决了我的很多问题。非常感谢你的帮助。
我正在尝试创建一个zip文件,以便能够通过http发送多个文件。 我的问题是,生成的Zip文件在发送之前和之后都“损坏”。问题是我无法找到我做错了什么,因为我在控制台中没有收到任何错误。 那么,有人有一个想法文件我生成的zip文件损坏? 这是我的代码: 谢谢你的帮助!
问题内容: 我们有一段代码可以在我们的系统上生成一个zip文件。一切正常,但是有时该Zip文件在由FilZip或WinZip打开时被视为已损坏。 所以这是我的问题:我们如何以编程方式检查生成的zip文件是否损坏? 这是我们用于生成zip文件的代码: 我们在这里做错了什么吗? 编辑:实际上,上面的代码是绝对可以的。我的问题是我正在为用户重定向WRONG流。因此,与其打开一个zip文件,不如打开一个完
问题内容: 我使用Eclipse在Windows 7中创建了一个jar文件。当我尝试打开jar文件时,它说jar文件无效或损坏。谁能建议我为什么jar文件无效? 问题答案: 当您在Windows资源管理器中双击一个JAR文件时,会发生这种情况,但是JAR本身实际上不是 可执行的 JAR。真正的可执行JAR至少应具有带有方法的类,并在中引用它。 在Eclispe中,您需要将项目导出为 Runnabl
问题内容: 我在使用Maven Tomcat7插件生成带有嵌入式Tomcat7实例的JAR存档时遇到问题。这是我的片段: 我的项目使用包装。生成了包含带有项目的WAR存档的Tomcat的JAR文件,但是当我尝试运行它时,出现错误: 我尝试了插件版本-相同的结果。 这是通过执行JAR提取的目录树: (如您所见,没有文件被复制) 问题答案: 从配置中删除路径。并使用2.1版的tomcat插件。在pom
Docx4J生成的Excel工作簿总是说损坏了,但我无法确定Excel不喜欢底层XML的什么,更不用说如何修复它了。 我的用例如下:我试图定期自动生成一个带有图表和图形的excel工作簿。只有原始数据会改变,但随着原始数据的改变,其他一切都会动态更新。 null null 在我的空白工作簿之前和之后 欢迎所有的想法。
问题内容: 我编写了一个代码,用于在文件中保存少量图像,然后压缩该文件并上传到ftp服务器。当我从服务器下载该文件时,很少有文件可用,并且很少有文件损坏。可能是什么原因呢?压缩代码或上载程序代码是否有故障。 压缩代码: } FTP上传代码: 问题答案: 确保传输BINARY_FILE_TYPE中的文件。也许返回假? 顺便说一句,如果您以ASCII模式传输zip,几乎肯定会导致损坏。