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

  • Home
  • SEARCH
  • 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 6628877
In Process

The Archive Base Latest Questions

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

I am trying to add a text file to a zip archive through a

  • 0

I am trying to add a text file to a zip archive through a Java program on Linux. The program spawns a process (using java.lang.Process) to execute the commandline “zip -j .zip .txt”, reads the output and error streams of the spawned process and waits for the process to complete using waitFor(). Though the program seems to run fine (spawned process exits with exit code 0, indicating that the zip commandline was executed successfully) and the output read from output and error streams do not indicate any errors, at the end of the program the zip archive doesn’t always contain the file supposed to have been added. This problem doesn’t happen consistently though (even with the same existing-archive and file-to-add) – once in a while (perhaps once in 4 attempts) the zip is found to have been updated correctly. Strangely, the problem doesn’t occur at all when the program is run through Eclipse debugger mode. Any pointers on why this problem occurs and how it can be addressed would be helpful. Thanks!

Below is the code snippet. The program calls addFileToZip(File, File, String):

public static void addFileToZip(final File zipFile, final File fileToBeAdded,
        final String fileNameToBeAddedAs) throws Exception {
    File tempDir = createTempDir();
    File fileToBeAddedAs = new File(tempDir, fileNameToBeAddedAs);
    try {
        FileUtils.copyFile(fileToBeAdded, fileToBeAddedAs);
        addFileToZip(zipFile, fileToBeAddedAs);
    } finally {
        deleteFile(fileToBeAddedAs);
        deleteFile(tempDir);
    }
}

public static void addFileToZip(final File zipFile, final File fileToBeAdded) throws Exception {
    final String[] command = {"zip", "-j", zipFile.getAbsolutePath(), fileToBeAdded.getAbsolutePath()};
    ProcessBuilder procBuilder = new ProcessBuilder(command);
    Process proc = procBuilder.start();
    int exitCode = proc.waitFor();
    /*
     * Code to read output/error streams of proc and log/print them
     */
    if (exitCode != 0) {
            throw new Exception("Unable to add file, error: " + errMsg);
    }
}
  • 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-25T22:10:49+00:00Added an answer on May 25, 2026 at 10:10 pm

    Make sure no other process has the zip file locked for write, or the file being added locked for read. If you’re generating the file to be added, make sure the stream is flushed and closed before spawning the zip utility.

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

Sidebar

Related Questions

I am trying to go line by line through text file using stream reader,
I am trying to add a line to a text file with Java. When
I'm trying to add some HTML formatted text to Word using Office Interop. My
I am trying to populate combo boxes from a text file using comma as
I am trying to read a text file using the code (pasted below), but
I am trying to add a text file to my project. Not that it
I am trying to make a text file in memory, add some lines to
I'm basically trying to create a linked list from a text file and add
I have a text file that I am trying to browse to using the
I am trying add two columns in a text file that are seperated by

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.