ZipWriterpublic class ZipWriter extends Object
Fields Summary |
---|
private String | zipFilename | private String | dirName | private ZipOutputStream | zipStream | private String[] | fileList | private byte[] | buffer |
Constructors Summary |
---|
public ZipWriter(String zipFilename, String dirName, String[] fileList)
try
{
// note -- these asserts will be caught & repackaged as a ZipFileException
Reporter.assertIt(zipFilename); //NOI18N
Reporter.assertIt(dirName); //NOI18N
Reporter.assertIt(fileList); //NOI18N
Reporter.assertIt(fileList.length > 0); //NOI18N
//make sure it's really a directory
File f = new File(dirName);
Reporter.assertIt(f.exists(), "directory (" + dirName + ") doesn't exist");//NOI18N
Reporter.assertIt(f.isDirectory()); //NOI18N
// change the filename to be full-path & UNIX style
try
{
dirName = f.getCanonicalPath();
}
catch(IOException e)
{
Reporter.warn("Couldn't getCanonicalPath() for " + dirName);//NOI18N
}
dirName = dirName.replace('\\", '/"); // all UNIX-style filenames...
// we need the dirname to end in a '/'
if(!dirName.endsWith("/"))//NOI18N
dirName += "/";//NOI18N
// make sure the zipFile requested isn't the name of an existing directory
f = new File(zipFilename);
Reporter.assertIt(!f.isDirectory(), "zipFile (" + zipFilename + ") is actually a directory!" );//NOI18N
for(int i = 0; i < fileList.length; i++)
{
fileList[i] = fileList[i].replace('\\", '/"); // just in case...
}
this.zipFilename = zipFilename;
this.dirName = dirName;
this.fileList = fileList;
zipStream = new ZipOutputStream(new FileOutputStream(zipFilename));
}
catch(Exception f)
{
Reporter.critical(new StackTrace(f)); //NOI18N
throw new ZipFileException(f);
}
| public ZipWriter(OutputStream outStream, String dirName, String[] fileList)
try
{
// note -- these asserts will be caught & repackaged as a ZipFileException
//Reporter.assertIt(zipFilename); //NOI18N
Reporter.assertIt(dirName); //NOI18N
Reporter.assertIt(fileList); //NOI18N
Reporter.assertIt(fileList.length > 0); //NOI18N
//make sure it's really a directory
File f = new File(dirName);
Reporter.assertIt(f.exists(), "directory (" + dirName + ") doesn't exist");//NOI18N
Reporter.assertIt(f.isDirectory()); //NOI18N
// change the filename to be full-path & UNIX style
try
{
dirName = f.getCanonicalPath();
}
catch(IOException e)
{
Reporter.warn("Couldn't getCanonicalPath() for " + dirName);//NOI18N
}
dirName = dirName.replace('\\", '/"); // all UNIX-style filenames...
// we need the dirname to end in a '/'
if(!dirName.endsWith("/"))//NOI18N
dirName += "/";//NOI18N
// make sure the zipFile requested isn't the name of an existing directory
//f = new File(zipFilename);
//Reporter.assertIt(!f.isDirectory(), "zipFile (" + zipFilename + ") is actually a directory!" );//NOI18N
for(int i = 0; i < fileList.length; i++)
{
fileList[i] = fileList[i].replace('\\", '/"); // just in case...
}
//this.zipFilename = zipFilename;
this.dirName = dirName;
this.fileList = fileList;
zipStream = new ZipOutputStream(outStream);
}
catch(Assertion.Failure f)
{
throw new ZipFileException(f);
}
|
Methods Summary |
---|
public void | addEntry(java.lang.String entryName)
int totalBytes = 0;
FileInputStream in = new FileInputStream(dirName + entryName);
ZipEntry ze = new ZipEntry(entryName);
zipStream.putNextEntry(ze);
for(int numBytes = in.read(buffer); numBytes > 0; numBytes = in.read(buffer))
{
zipStream.write(buffer, 0, numBytes);
totalBytes += numBytes;
}
zipStream.closeEntry();
Reporter.verbose("Wrote " + entryName + " to Zip File. Wrote " + totalBytes + " bytes.");//NOI18N
| public static void | main(java.lang.String[] notUsed)
Reporter.setSeverityLevel(0); //NOI18N
try
{
String[] array = { "hello.txt", "a\\a.txt", "a\\b/b.txt" };//NOI18N
ZipWriter zw = new ZipWriter("E:\\temp\\hello/ZipWriter.jar", "E:/Temp\\hello", array);//NOI18N
zw.write();
Reporter.verbose("" + zw);//NOI18N
}
catch(ZipFileException e)
{
Reporter.verbose("ZipFileException: " + e);//NOI18N
}
| public java.lang.String | toString()
String s = "Zip File Name: " + zipFilename + "\n";//NOI18N
s += "Directory Name: " + dirName + "\n";//NOI18N
s += "***** File Contents *********\n";//NOI18N
s += ContainerHelper.toOneString(fileList);
return s;
| public void | write()
try
{
//zipStream = new ZipOutputStream(new FileOutputStream(zipFilename));
for(int i = 0; i < fileList.length; i++)
{
addEntry(fileList[i]);
}
zipStream.close();
}
catch(ZipFileException z)
{
Reporter.critical(new StackTrace(z)); //NOI18N
throw z;
}
catch(Exception e)
{
Reporter.critical(new StackTrace(e)); //NOI18N
throw new ZipFileException(e);
}
|
|