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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:23:22+00:00 2026-06-17T12:23:22+00:00

Possible Duplicate: Appending files to a zip file with Java Hello Java Developers, Here’s

  • 0

Possible Duplicate:
Appending files to a zip file with Java

Hello Java Developers,

Here’s the scenario:

Say I have a textfile named sample.txt. What I actually want to do is to put the sample.txt file into a *.zip file named TextFiles.zip.

Here’s what I have learned so far.

try{
    File f = new File(compProperty.getZIP_OUTPUT_PATH());
    zipOut = new ZipOutputStream(new FileOutputStream(f));
    ZipEntry zipEntry = new ZipEntry("sample.txt");
    zipOut.putNextEntry(zipEntry);
    zipOut.closeEntry();
    zipOut.close();
    System.out.println("Done");

} catch ( Exception e ){
    // My catch block
}

My code so far creates a *.zip file and insert the sample.txt file.
My question is how would I be able to insert an existing file to the created *.zip file?
If your answer has anything to do with TrueZIP, please post an SSCCE.

I have done the following:

  • Googled
  • Search for existing question. ( Found few. No answer. Some didn’t answer my particular question.
  • Read TrueZip. Yet, I couldn’t understand a thing. ( Please do understand )
  • 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-17T12:23:22+00:00Added an answer on June 17, 2026 at 12:23 pm

    Using the inbuilt Java API. This will add a file to a Zip File, this will replace any existing Zip files that may exist, creating a new Zip file.

    public class TestZip02 {
    
      public static void main(String[] args) {
        try {
          zip(new File("TextFiles.zip"), new File("sample.txt"));
        } catch (IOException ex) {
          ex.printStackTrace();
        }
      }
    
      public static void zip(File zip, File file) throws IOException {
        ZipOutputStream zos = null;
        try {
          String name = file.getName();
          zos = new ZipOutputStream(new FileOutputStream(zip));
    
          ZipEntry entry = new ZipEntry(name);
          zos.putNextEntry(entry);
    
          FileInputStream fis = null;
          try {
            fis = new FileInputStream(file);
            byte[] byteBuffer = new byte[1024];
            int bytesRead = -1;
            while ((bytesRead = fis.read(byteBuffer)) != -1) {
              zos.write(byteBuffer, 0, bytesRead);
            }
            zos.flush();
          } finally {
            try {
              fis.close();
            } catch (Exception e) {
            }
          }
          zos.closeEntry();
    
          zos.flush();
        } finally {
          try {
            zos.close();
          } catch (Exception e) {
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Appending files to a zip file with Java I have a zip
Possible Duplicate: byte[] array pattern search Let's say I have an array of bytes:
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: DOMElement cloning and appending: ‘Wrong Document Error’ I would like to copy
Possible Duplicate: Choosing Java vs Python on Google App Engine We are going to
Possible Duplicate: .eof() loop not working So I have a fairly simple C++ program
I have written a service that monitors a file drop location for files from
Possible Duplicate: Run python script without DOS shell appearing I have a python script
Possible Duplicate: Unexpected value from nativeGetEnabledTags: 0 I have just installed the latest version
Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have a HTML

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.