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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:37:48+00:00 2026-05-25T09:37:48+00:00

I have a method to zip files in Java: public void compress(File[] inputFiles, OutputStream

  • 0

I have a method to zip files in Java:

public void compress(File[] inputFiles, OutputStream outputStream) {

    Validate.notNull(inputFiles, "Input files are required");
    Validate.notNull(outputStream, "Output stream is required");

    int BUFFER = 2048;

    BufferedInputStream origin = null;

    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
    byte data[] = new byte[BUFFER];

    for (File f : inputFiles) {
        FileInputStream fi;
        try {
            fi = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Input file not found", e);
        }
        origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(f.getName());
        try {
            out.putNextEntry(entry);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int count;
        try {
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            origin.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    try {
        out.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

As you can see parameter inputFiles is an Array of File objects. This all works, but I’d like to have instead a collection of InputStream objects as parameter to make it more flexible.

But then I have the problem that when making a new ZipEntry (as in code above)

ZipEntry entry = new ZipEntry(f.getName());

I don’t have a filename to give as parameter.

How should i solve this? Maybe a Map with (fileName,inputStream) pairs?

Any thoughts on this are appreciated!

Thanks,
Nathan

  • 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-05-25T09:37:49+00:00Added an answer on May 25, 2026 at 9:37 am

    I think your suggestion Map<String, InputStream> is a good solution.

    Just a side note: Remember to close the inputstreams after you are done


    If you want to make it more “fancy” you can always use create an interface:

    interface ZipOuputInterface {
        String getName();
        InputStream getInputStream();
    }
    

    And have it implemented differently in your different cases for instance File:

    class FileZipOutputInterface implements ZipOutputInterface {
    
        File file;
    
        public FileZipOutputInterface(File file) {
            this.file = file;
        }
    
        public String getName() {
            return file.getAbstractName();
        }
        public InputStream getInputStream() {
            return new FileInputStream(file);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

currently I have a java downloader that downloads a .zip file and uncompresses it,
I have a method which takes params object[] such as: void Foo(params object[] items)
I'm in a VB.NET application. I have referenced some java.* namespaces in my file
I have a zip file which can contain any number of zipfiles inside it
I have very strange problem when I upload files to my FTP (zip or
In my current iPhone project I need to decrypt zip files which have been
I have a Zip archive with a large (important) file that will not extract.
I am writing a utility that will zip a file (or set of files)
I have written a multi-threaded Java application which reads a bunch of .jar files
I have this code that's supposed to unzipp a file. public class dec extends

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.