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 8227727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:13:27+00:00 2026-06-07T16:13:27+00:00

I have tried multiple ways to create this zip file in Java/Groovy. The first

  • 0

I have tried multiple ways to create this zip file in Java/Groovy. The first couple methods I attempted, from various blogs/postings, resulted in corrupt zip files which could not be opened. So, I tried this one (below) which looked fairly promising. The sysouts report valid file paths being passed to the FileInputStream. I am not sure if it is the FQ path being passed to the ZipOutputStream which is causing the problem. Either way, below is the code, which results in small (188kb) zip file (with no entries) being created. Any suggestions?

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

class FileZipper {

    public static void makeZip(Set fullyQualifiedFileNames, String zipFileName, String outDir) throws IOException, FileNotFoundException
    {
        // These are the files to include in the ZIP file
        Object[] filenames = fullyQualifiedFileNames.toArray();
        String fileSeparator =  (String) System.getProperties().get("file.separator");

        // Create a buffer for reading the files
        byte[] buf = new byte[1024];

        // Create the ZIP file
        String outFilename = outDir + fileSeparator +zipFileName;
        FileOutputStream fos = new FileOutputStream(outFilename);
        ZipOutputStream zos = new ZipOutputStream(fos);
        System.out.println("Zipping to file " +outFilename);
        // Compress the files

        for (Object fileName: filenames)
        {
            System.out.println("Adding file: " + fileName);
            FileInputStream fis = new FileInputStream((String)fileName);

            // Add ZIP entry to output stream.
            String[] nodes = ((String)fileName).split("[/[\\\\]]");
            String zipEntry = nodes[nodes.length-1];
            System.out.println("Adding Zip Entry: " + zipEntry);
            zos.putNextEntry(new ZipEntry((String)fileName));

            // Transfer bytes from the file to the ZIP file
            int len;
            int totalBytes = 0;
            while ((len = fis.read(buf)) > 0) 
            {
                totalBytes += len;
                zos.write(buf, 0, len);
            }
            System.out.println("Zipped " +totalBytes +" bytes");
            // Complete the entry
            zos.closeEntry();
            fis.close();
        }

        // Complete the ZIP file
        zos.close();
        fos.close();
    }
}
  • 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-07T16:13:28+00:00Added an answer on June 7, 2026 at 4:13 pm

    If you are using Groovy, the easiest way is using AntBuilder:

    new AntBuilder().zip(
       destfile: "myfile.zip",
       basedir: "baseDir")
    

    or as of Groovy 1.8:

    ant.zip(destfile: 'file.zip', basedir: 'src_dir')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried this a couple ways, the first is have a class that implements
I have tried this in multiple ways: if-then-else, guards, case statements but I never
Im getting an error using this code below. I have tried multiple ways and
I've tried multiple ways to block a folder of logs from Mercurial but it
There appears to be multiple ways to do this, but the examples I've tried
I have tried multiple ways of grabbing the username of a current logged on
I am feeling stupid but have tried multiple ways of having new line in
I understand there are MANY posts on this, but I have tried every single
I am trying to crop an image. I have found multiple ways to do
I have created one user control for multiple file upload , i need to

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.