arctanx
asked on
How to add files to an existing zip file?
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.FileNotFoundExcept ion;
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.ZipOutputStr eam;
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(zipFileNa me));
out.setLevel(Deflater.DEFA ULT_COMPRE SSION);
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().endsWit h(ext);
}
});
}
}
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.FileNotFoundExcept
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.ZipOutputStr
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(zipFileNa
out.setLevel(Deflater.DEFA
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().endsWit
}
});
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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
Yes.
You might find that using Ant programatically makes things a bit easier
ASKER
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?
Or would it be better to create a temp directory, do the work there, and then trash the whole directory?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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
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
ASKER
Thanks fellas.
This info will get me what I need.
This info will get me what I need.
:-)
ASKER
There's not a way I can just add one file to an archive already there?
This sounds like alot of work.