Solved

How to add files to an existing zip file?

Posted on 2006-06-22
8
428 Views
Last Modified: 2012-05-05
I need to add files to an already existing zip file, here's what I have so far, this searches for files of a certain ext and zips them up.

I would like to maybe pass in a file name, and add that to an already existing zip file.

I have no idea where to start.

help.

thanks.

---------------------------------------------------------



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipIt
{
    private static final String dir = "C:\\Temp\\";
    private static final String ext = ".TMP";

    public ZipIt()
    {        
    }

    public static void main(String[] args)
    {
        ZipIt z = new ZipIt();
        String[] filesToZip = z.getFilesToZip();

        byte[] buffer = new byte[18024];
        String zipFileName = dir + "example.zip";
        try
        {
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
            out.setLevel(Deflater.DEFAULT_COMPRESSION);
            for (int i = 0; i < filesToZip.length; i++)
            {
                FileInputStream in = new FileInputStream(dir + filesToZip[i]);
                out.putNextEntry(new ZipEntry(dir + filesToZip[i]));
                int len;
                while ((len = in.read(buffer)) > 0)
                {
                    out.write(buffer, 0, len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
        }
        catch (IllegalArgumentException iae)
        {
            iae.printStackTrace();
        }
        catch (FileNotFoundException fnfe)
        {
            fnfe.printStackTrace();
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

    private String[] getFilesToZip()
    {
        return new File(dir).list(
                new FilenameFilter()
                {
                    public boolean accept(File dir, String name)
                    {
                        return name.toUpperCase().endsWith(ext);
                    }
                });
    }
}
0
Comment
Question by:arctanx
  • 4
  • 3
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 75 total points
ID: 16964245
You'll need to make a new archive, add all, then delete and rename if necessary
0
 
LVL 2

Author Comment

by:arctanx
ID: 16964355
You mean make a new archive, unzip the old one, THEN add all?

There's not a way I can just add one file to an archive already there?

This sounds like alot of work.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16964407
>>You mean make a new archive, unzip the old one, THEN add all?

Yes.

You might find that using Ant programatically makes things a bit easier
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 2

Author Comment

by:arctanx
ID: 16964428
So then that means I'll need to delete the original archive first, then rename the new archive the old name?

Or would it be better to create a temp directory, do the work there, and then trash the whole directory?

0
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 16964441
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16964446
>>So then that means I'll need to delete the original archive first, then rename the new archive the old name?

Yes

>>Or would it be better to create a temp directory, do the work there, and then trash the whole directory?

If you don't mind the same file name in a different directory, then yes
0
 
LVL 2

Author Comment

by:arctanx
ID: 16964470
Thanks fellas.

This info will get me what I need.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16964503
:-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 52
mysql jsp example issue 32 75
Is Applet the way to go for my drag and drop system? 8 35
netbeans can not run self signed app 8 14
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question