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

The Archive Base Latest Questions

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

Possible Duplicate: Appending files to a zip file with Java I have a zip

  • 0

Possible Duplicate:
Appending files to a zip file with Java

I have a zip that contains a few folders in it but the important one is dir and inside that is another folder called folder and folder contains a lot of files that I need to be able to update.

I have now a dir outside of the zip called dir and in that is folder with the files i need to update in so the paths are the same. How can i update those files into the zip?

The tricky part is that dir is at the root of the zip and it contains a lot of folders not just folder but i only need to update the files in folder i can’t mess with any of the files out side of folders but still in dir.

Can this be done? I know this can be done in bash using the -u modifier but I would prefer to do this with java if it’s possible.

Thank you for any help with this issue

Just to be clearer

Inside Zip
/dir/folder/filestoupdate

Outside the zip
/dir/folder/filestomoveintozip

  • 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-30T09:23:38+00:00Added an answer on May 30, 2026 at 9:23 am

    Alright well here is the final method it’s the same method i pastebinned before which i actually got from the stackoverflow topic in the link @Qwe posted before but i added the path variable so that it could add files to folders inside the zip

    Alright so now how to use it in my example above i wanted to add a file into a folder that was inside another folder i would do that using my setup in the question like this

    private void addFilesToZip(File source, File[] files, String path){
        try{
            File tmpZip = File.createTempFile(source.getName(), null);
            tmpZip.delete();
            if(!source.renameTo(tmpZip)){
                throw new Exception("Could not make temp file (" + source.getName() + ")");
            }
            byte[] buffer = new byte[4096];
            ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip));
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(source));
            for(int i = 0; i < files.length; i++){
                InputStream in = new FileInputStream(files[i]);
                out.putNextEntry(new ZipEntry(path + files[i].getName()));
                for(int read = in.read(buffer); read > -1; read = in.read(buffer)){
                    out.write(buffer, 0, read);
                }
                out.closeEntry();
                in.close();
            }
            for(ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()){
                if(!zipEntryMatch(ze.getName(), files, path)){
                    out.putNextEntry(ze);
                    for(int read = zin.read(buffer); read > -1; read = zin.read(buffer)){
                        out.write(buffer, 0, read);
                    }
                    out.closeEntry();
                }
            }
            out.close();
            tmpZip.delete();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    private boolean zipEntryMatch(String zeName, File[] files, String path){
        for(int i = 0; i < files.length; i++){
            if((path + files[i].getName()).equals(zeName)){
                return true;
            }
        }
        return false;
    }
    

    Thanks for the link ended up being able to improve that method a bit so that it could add in files that weren’t in the root and now i’m a happy camper 🙂 hope this helps someone else out as well

    EDIT
    I worked a bit more on the method so that it could not only append to the zip but it also is able to update files within the zip

    Use the method like this

    File[] files = {new File("/path/to/file/to/update/in")};
    addFilesToZip(new File("/path/to/zip"), files, "folder/dir/");
    

    You wouldn’t start the path (last variable) with / as that’s not how it’s listed in the zip entries

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

Sidebar

Related Questions

I have written a service that monitors a file drop location for files from
Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: Working with latitude/longitude values in Java Duplicate: Working with latitude/longitude values in
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: Run python script without DOS shell appearing I have a python script
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I make a list with checkboxes in Java Swing? I
Possible Duplicate: What does appending “?v=1” to CSS and Javascript URLs in link and

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.