Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7778245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:20:59+00:00 2026-06-01T18:20:59+00:00

I am trying to add some files to a ZIP file, it creates the

  • 0

I am trying to add some files to a ZIP file, it creates the file but does not add anything into it.
Code 1:

String fulldate = year + "-" + month + "-" + day + "-" + min;

File dateFolder = new File("F:\\" + compname + "\\" + fulldate);
dateFolder.mkdir();

String zipName = "F:\\" + compname + "\\" + fulldate + "\\" + fulldate + ".zip";

zipFolder(tobackup, zipName);

My function:

public static void zipFolder(File folder, String name) throws Exception {
    byte[] buffer = new byte[18024];

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(name));
    FileInputStream in = new FileInputStream(folder);

    out.putNextEntry(new ZipEntry(name));

    int len;

    while((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }

    out.closeEntry();
    in.close();
    out.close();
}

Edit: I found the problem, it was just having trouble writing files from the C:\ drive into a ZIP in the F:\ drive

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:21:01+00:00Added an answer on June 1, 2026 at 6:21 pm

    You can’t zip folders, only files. To zip folders, you have to add all the subfiles manually. I wrote this class that does the job. You can have it for free 🙂

    The usage would be this:

    List<File> sources = new ArrayList<File>();
    sources.add(tobackup);
    Packager.packZip(new File(zipName), sources);
    

    Here is the class:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.List;
    import java.util.zip.Deflater;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    public class Packager
    {
        public static void packZip(File output, List<File> sources) throws IOException
        {
            System.out.println("Packaging to " + output.getName());
            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
            zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
    
            for (File source : sources)
            {
                if (source.isDirectory())
                {
                    zipDir(zipOut, "", source);
                } else
                {
                    zipFile(zipOut, "", source);
                }
            }
            zipOut.flush();
            zipOut.close();
            System.out.println("Done");
        }
    
        private static String buildPath(String path, String file)
        {
            if (path == null || path.isEmpty())
            {
                return file;
            } else
            {
                return path + "/" + file;
            }
        }
    
        private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException
        {
            if (!dir.canRead())
            {
                System.out.println("Cannot read " + dir.getCanonicalPath() + " (maybe because of permissions)");
                return;
            }
    
            File[] files = dir.listFiles();
            path = buildPath(path, dir.getName());
            System.out.println("Adding Directory " + path);
    
            for (File source : files)
            {
                if (source.isDirectory())
                {
                    zipDir(zos, path, source);
                } else
                {
                    zipFile(zos, path, source);
                }
            }
    
            System.out.println("Leaving Directory " + path);
        }
    
        private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException
        {
            if (!file.canRead())
            {
                System.out.println("Cannot read " + file.getCanonicalPath() + " (maybe because of permissions)");
                return;
            }
    
            System.out.println("Compressing " + file.getName());
            zos.putNextEntry(new ZipEntry(buildPath(path, file.getName())));
    
            FileInputStream fis = new FileInputStream(file);
    
            byte[] buffer = new byte[4092];
            int byteCount = 0;
            while ((byteCount = fis.read(buffer)) != -1)
            {
                zos.write(buffer, 0, byteCount);
                System.out.print('.');
                System.out.flush();
            }
            System.out.println();
    
            fis.close();
            zos.closeEntry();
        }
    }
    

    Enjoy!

    EDIT: To check if the program is still busy, you can add the three lines I marked with a (*)

    EDIT 2: Try the new code. On my platform, it runs correct (OS X). I’m not sure but, there might be some limited read permissions for files in Windows in AppData.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating some new files and am trying to add them but when I
Im trying to add some short file names to a Wise installer to fix
I'm trying to subclass the built-in file class in Python to add some extra
I'm trying to add some accessibility for screen readers into a Flash application, and
I'm trying to add some security to my ASP.NET 1.0 MVC app (VB), but
I`m trying to add cache headers on my static files (.css, .js), but only
I am trying to add some filtering to the application context file, which resides
I'm trying to add some extended error codes to the event log but I
I'm trying to add the T4MVC templates to my project, but I'm experiencing some
I'm trying to insert some files into a Postgres database. Since lots of duplication

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.