Java压缩中的FileNotFound错误

时间:2015-05-13 07:29:26

标签: java file io zip inputstream

我正在尝试使用Java压缩文件。我的要求是压缩任何东西,这意味着,

  1. 文件夹
  2. 文件夹
  3. 文件
  4. 文件
  5. 包含子文件夹的文件夹
  6. 文件(不在文件夹中)和文件夹
  7. 您几乎可以在包括winzip在内的所有压缩程序中看到上述要求。

    以下是我的尝试。

    Compress.java

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    /**
     *
     * @author user
     */
    public class Compress
    {
    
        private List<String> fileList;
        private static final String SOURCE_FOLDER = ExpressWizard.filePath.getPath();
        private static final String OUTPUT_ZIP_FILE = ExpressWizard.destination.getPath();
    
        public Compress()
        {
            fileList = new ArrayList<String>();
            System.out.println(SOURCE_FOLDER + "\n");
            System.out.println(OUTPUT_ZIP_FILE);
    
        }
    
        public Compress(File[] list)
        {
            fileList = new ArrayList<String>();
            System.out.println(SOURCE_FOLDER + "\n");
            System.out.println(OUTPUT_ZIP_FILE);
    
        }
    
    
    
        public void zipIt(String zipFile, File[] list)
        {
            byte[] buffer = new byte[1024];
            String source = "";
            ZipOutputStream zos=null;
    
    
            try
            {
                FileOutputStream fos = new FileOutputStream(zipFile);
                zos = new ZipOutputStream(fos);
    
                for (int i = 0; i < list.length; i++)
                {
                    String SOURCE_FOLDER = list[i].getPath();
    
                    try
                    {
                        source = SOURCE_FOLDER.substring(SOURCE_FOLDER.lastIndexOf("\\") + 1, SOURCE_FOLDER.length());
                    } catch (Exception e)
                    {
                        source = SOURCE_FOLDER;
                    }
    
    
                    System.out.println("Output to Zip : " + zipFile);
                    FileInputStream in = null;
    
                    for (String file : this.fileList)
                    {
                        System.out.println("File Added : " + file);
                        ZipEntry ze = new ZipEntry(source + File.separator + file);
                        zos.putNextEntry(ze);
                        try
                        {
                            in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
                            int len;
                            while ((len = in.read(buffer)) > 0)
                            {
                                zos.write(buffer, 0, len);
                            }
                        } finally
                        {
                            in.close();
                        }
                    }
                }
    
                zos.closeEntry();
                System.out.println("Folder successfully compressed");
    
            } catch (IOException ex)
            {
                ex.printStackTrace();
            } finally
            {
                try
                {
                    zos.close();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    
        public void generateFileList(File node)
        {
    
            // add file only
            if (node.isFile())
            {
                System.out.println("File executed");
                fileList.add(generateZipEntry(node.toString()));
    
            }
    
            if (node.isDirectory())
            {
                System.out.println("Folder executed");
                String[] subNote = node.list();
                for (String filename : subNote)
                {
                    generateFileList(new File(node, filename));
                }
            }
        }
    
        public void generateFileList(File[] node)
        {
    
            for (int i = 0; i < node.length; i++)
            {
                // add file only
                if (node[i].isFile())
                {
                    System.out.println("File executed");
                    fileList.add(generateZipEntry(node.toString()));
    
                }
    
                if (node[i].isDirectory())
                {
                    System.out.println("Folder executed");
                    String[] subNote = node[i].list();
                    for (String filename : subNote)
                    {
                        generateFileList(new File(node[i], filename));
                    }
                }
            }
    
        } 
    
        private String generateZipEntry(String file)
        {
            return file.substring(SOURCE_FOLDER.length() + 1, file.length());
        }
    
    }
    

    这就是我们如何称呼上述课程。

    ExpressWizard.java

    private void backUpButtonActionPerformed(java.awt.event.ActionEvent evt)                                             
        {                                                 
            try
            {
                Compress compress = new Compress();
                if (CompressType.ZIP==zip)
                {    
                    compress.generateFileList(serlectFiles);
                    System.out.println(destination.getPath());
                    compress.zipIt(destination.getPath(),serlectFiles);
    
                }
            } catch (Exception ex)
            {
                JOptionPane.showMessageDialog(this, "Error in creating a zip compression ! ");
                ex.printStackTrace();
    
            }
        }     
    

    此代码生成以下错误

    java.io.FileNotFoundException: C:\Users\user\Desktop\contact\drivingLicense.png (The system cannot find the file specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:195)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at java.io.FileInputStream.<init>(FileInputStream.java:93)
        at com.theace.backupsystem.compression.Compress.zipIt(Compress.java:138)
        at com.theace.backupsystem.view.ExpressWizard.backUpButtonActionPerformed(ExpressWizard.java:2781)
        at com.theace.backupsystem.view.ExpressWizard.access$5900(ExpressWizard.java:31)
        at com.theace.backupsystem.view.ExpressWizard$40.actionPerformed(ExpressWizard.java:2021)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
        at java.awt.Component.processMouseEvent(Component.java:6525)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
        at java.awt.Component.processEvent(Component.java:6290)
        at java.awt.Container.processEvent(Container.java:2234)
        at java.awt.Component.dispatchEventImpl(Component.java:4881)
        at java.awt.Container.dispatchEventImpl(Container.java:2292)
        at java.awt.Component.dispatchEvent(Component.java:4703)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
        at java.awt.Container.dispatchEventImpl(Container.java:2278)
        at java.awt.Window.dispatchEventImpl(Window.java:2750)
        at java.awt.Component.dispatchEvent(Component.java:4703)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:702)
        at java.awt.EventQueue$3.run(EventQueue.java:696)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
        at java.awt.EventQueue$4.run(EventQueue.java:724)
        at java.awt.EventQueue$4.run(EventQueue.java:722)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109)
        at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:184)
        at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:229)
        at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:227)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:227)
        at java.awt.Dialog.show(Dialog.java:1084)
        at java.awt.Component.show(Component.java:1654)
        at java.awt.Component.setVisible(Component.java:1606)
        at java.awt.Window.setVisible(Window.java:1014)
        at java.awt.Dialog.setVisible(Dialog.java:1005)
        at com.theace.backupsystem.view.HomePanel.clickPanel01MouseClicked(HomePanel.java:653)
        at com.theace.backupsystem.view.HomePanel.access$000(HomePanel.java:16)
        at com.theace.backupsystem.view.HomePanel$1.mouseClicked(HomePanel.java:120)
        at java.awt.Component.processMouseEvent(Component.java:6528)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
        at java.awt.Component.processEvent(Component.java:6290)
        at java.awt.Container.processEvent(Container.java:2234)
        at java.awt.Component.dispatchEventImpl(Component.java:4881)
        at java.awt.Container.dispatchEventImpl(Container.java:2292)
        at java.awt.Component.dispatchEvent(Component.java:4703)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4542)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
        at java.awt.Container.dispatchEventImpl(Container.java:2278)
        at java.awt.Window.dispatchEventImpl(Window.java:2750)
        at java.awt.Component.dispatchEvent(Component.java:4703)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:702)
        at java.awt.EventQueue$3.run(EventQueue.java:696)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
        at java.awt.EventQueue$4.run(EventQueue.java:724)
        at java.awt.EventQueue$4.run(EventQueue.java:722)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    

    当我们选择多个文件夹时,基本上会发生错误。错误是正确的,它表示drivingLicense.png不在C:\Users\user\Desktop\contact\路径中。是的,它是C:\Users\user\Desktop\screens\,它是我选择的另一个文件夹。

    上面的代码似乎搞乱了路径,我该如何解决?

    除此之外,我们如何安排这些代码来压缩我描述为“要求”的文件,文件夹等?

1 个答案:

答案 0 :(得分:1)

你的问题是你忘了&#34;子文件夹的目录名称:

private String generateZipEntry(String file)
{
    return file.substring(SOURCE_FOLDER.length() + 1, file.length());
}

这将始终为目录SOURCE_FOLDER中的文件创建条目。但是你递归地叫generateFileList()

        if (node[i].isDirectory())
        {
            System.out.println("Folder executed");
            String[] subNote = node[i].list();
            for (String filename : subNote)
            {
                // here, you pass a subdirectory, but in
                // generateZipEntry() you always append
                // filename to SOURCE_FOLDER, and not the
                // directoy the file is in
                generateFileList(new File(node[i], filename));
            }
        }

您应该将directoy和filename(或更好的File对象)传递给generateZipEntry