FileNotFound error in Java compression

I am trying to zip files using Java. My requirement is to anchor everything that means

  • Folder
  • Folders
  • File
  • Files
  • Folders with additional folders
  • Files (that are not in the folder) and folders

You can see this requirement pretty well in almost every compression program, including winzip.

Below I have tried.

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());
    }

}

      

This is what we call the above class.

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();

        }
    }     

      

This code generated below error

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)

      

The error mostly occurs when more than one folder is selected. And the error is correct, it says that drivingLicense.png

it is not on the way C:\Users\user\Desktop\contact\

. Yes, that's right, it's in C:\Users\user\Desktop\screens\

, which is the other folder I selected.

The above code seems to be confusing with paths, how can I fix it?

Also, how can we organize this code to compress files, folders, etc. that I described as "requirements"?

+3


source to share


1 answer


Your problem is that you "forgot" the directory name for the subfolders:

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

      

This will always create an entry for the file in the directory SOURCE_FOLDER

. But you are calling generateFileList()

recursively:



        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));
            }
        }

      

You should pass directoy and filename (or better, an object File

) to generateZipEntry

.

+1


source







All Articles