FileDocCategorySizeDatePackage
ZipFile.javaAPI DocGlassfish v2 API12908Fri May 04 22:34:58 BST 2007com.sun.enterprise.tools.common.util.zip

ZipFile

public class ZipFile extends Object

Fields Summary
private String
zipFilename
private String
explodeDirName
private File
zipFile
private File
explodeDir
private ArrayList
files
private static final String
specialDir
private byte[]
buffer
private ZipInputStream
zipStream
Constructors Summary
public ZipFile(String zipFilename, String explodeDirName)

		assertIt(zipFilename);
		assertIt(explodeDirName);

		this.zipFilename	= zipFilename;
		this.explodeDirName	= explodeDirName;

		try
		{
			checkZipFile();
                        zipStream = new ZipInputStream(new FileInputStream(zipFile));
                        //this(zipStream, explodeDirName);
			checkExplodeDir();
		}
		catch(Exception f)
		{
			throw new ZipFileException(f);
		}
	
public ZipFile(InputStream inStream, String explodeDirName)

		//assertIt(zipFilename);
		assertIt(explodeDirName);

		//this.zipFilename	= zipFilename;
		this.explodeDirName	= explodeDirName;

		try
		{
			//checkZipFile();
                    zipStream = new ZipInputStream(inStream);
			checkExplodeDir();
		}
		catch(Assertion.Failure f)
		{
			throw new ZipFileException(f);
		}
	
Methods Summary
private static voidassertIt(java.lang.String s)

		if( s == null || s.length() < 0 )
			throw new ZipFileException();
		else
			return;
	
private static voidassertIt(java.lang.String s, java.lang.String mesg)

		if( s == null || s.length() < 0 )
			throw new ZipFileException( mesg );
		else
			return;
	
private static voidassertIt(boolean b)

		if( !b )
			throw new ZipFileException();
		else
			return;
	
private static voidassertIt(boolean b, java.lang.String mesg)

		if( !b )
			throw new ZipFileException( mesg );
		else
			return;
	
private voidcheckExplodeDir()

		File dir;

		assertIt(explodeDirName);
		explodeDir = new File(explodeDirName);
		
		assertIt(explodeDir.exists(),		"Target Directory doesn't exist: " + explodeDirName );//NOI18N
		assertIt(explodeDir.isDirectory(),	"Target Directory isn't a directory: " + explodeDirName );//NOI18N
		assertIt(explodeDir.canWrite(),		"Can't write to Target Directory: " + explodeDirName );//NOI18N
	
private voidcheckZipFile()

		assertIt(zipFilename);
		zipFile = new File( zipFilename );
		assertIt( zipFile.exists(), "zipFile (" + zipFilename + ") doesn't exist" );//NOI18N
		assertIt( !zipFile.isDirectory(), "zipFile (" + zipFilename + ") is actually a directory!" );//NOI18N
		//createFileNameList();
	
private voidcreateDirs()

		// go through the array of filenames from the zip -- and create the required directory
		// structure in the explode directory...

		Assertion.check(explodeDir, "Programmer Error -- need to setup explodeDir");//NOI18N
		Iterator iter = files.iterator();
		
		while(iter.hasNext())
		{
			String	fname		= (String) iter.next();
			File	fullpath	= new File(explodeDir, fname);
			File	newDir		= fullpath.getParentFile();
			
			if(newDir.mkdirs())
			{	// note:  it returns false if dir already exists...
				Reporter.verbose("Created new directory:  " + newDir);//NOI18N
			}

			if(fullpath.delete())	// wipe-out pre-existing files
				Reporter.info("deleted pre-existing file: " + fullpath); //NOI18N
			
			assertIt(newDir.exists() && newDir.isDirectory(), "Couldn't create directory: " + newDir);//NOI18N
		}
	
private voidcreateFileNameList()
/******************************** Private ****************************** /

		ZipInputStream zin;

		assertIt( (files != null) ? false : true, "createFileNameList() called a second time.  Should only be called once and only once!" );//NOI18N
		files = new ArrayList();
		zin = null;
		try
		{
			ZipEntry ze;

			zin = zipStream; // new ZipInputStream( new FileInputStream( zipFile ) );
			while( (ze = zin.getNextEntry()) != null )
			{
				String name = ze.getName();
				zin.closeEntry();
				files.add(name);
			}
			zin.close();
		}
		catch( IOException e)
		{
			Reporter.error(e + "  " + zipFile);//NOI18N
			throw new ZipFileException(e);
		}
	
public java.lang.String[]explode()

		ArrayList explodedFiles = new ArrayList();

		//createDirs();

		ZipInputStream zin = null;

		// OK -- at this point, we have a good zip file, and a list of all the files in the zip file.
		// We've created all the subdirectories needed to explode the files into.
		// let's get busy...

		try
		{
			zin = zipStream; // new ZipInputStream(new FileInputStream(zipFile));
			ZipEntry ze;

			while( (ze = zin.getNextEntry()) != null )
			{
				String filename = ze.getName();
				
				if(isManifest(filename))
				{
					continue;	// don't bother with manifest file...
				}
									
                                File	fullpath	= new File(explodeDir, filename);
                                File	newDir		= fullpath.getParentFile();
			
                                if(newDir.mkdirs())
                                {	// note:  it returns false if dir already exists...
                                    Reporter.verbose("Created new directory:  " + newDir);//NOI18N
                                }

                                if(fullpath.delete())	// wipe-out pre-existing files
                                    Reporter.info("deleted pre-existing file: " + fullpath); //NOI18N
			
			//assertIt(newDir.exists() && newDir.isDirectory(), "Couldn't create directory: " + newDir);//NOI18N
				FileOutputStream os = getOutputStream(filename);

				if(os == null)	// e.g. if we asked to write to a directory instead of a file...
					continue;

				int totalBytes = 0;

				for(int numBytes = zin.read(buffer); numBytes > 0; numBytes = zin.read(buffer))
				{
					os.write(buffer, 0, numBytes);
					totalBytes += numBytes;
				} 
				os.close();				
				Reporter.verbose("Wrote " + totalBytes + " to " + filename);//NOI18N
				explodedFiles.add(filename);
			}
		}
		catch(IOException e)
		{
			throw new ZipFileException(e);
		}
		finally
		{
			Reporter.verbose("Closing zin...");//NOI18N
			try 
			{ 
				zin.close(); 
			}
			catch(IOException e) 
			{
				throw new ZipFileException("Got an exception while trying to close Jar input stream: " + e);//NOI18N
			}
		}
		Reporter.info("Successfully Exploded " + zipFilename + " to " + explodeDirName);//NOI18N
		return ContainerHelper.toStringArray(explodedFiles);
	
private java.io.FileOutputStreamgetOutputStream(java.lang.String filename)

		Assertion.check(explodeDir, "Programmer Error -- need to setup explodeDir");//NOI18N
		File f = new File(explodeDir, filename);

		if(f.isDirectory())
		{
			Reporter.warn("Weird!  A directory is listed as an entry in the jar file -- skipping...");//NOI18N
			return null;
		}

		try
		{
			return new FileOutputStream(f);
		}
		catch(FileNotFoundException e)
		{
			throw new ZipFileException(e);
		}
		catch(IOException e)
		{
			throw new ZipFileException(e);
		}
	
private booleanisManifest(java.lang.String filename)

		if(filename.toLowerCase().endsWith("manifest.mf"))//NOI18N
			return false;
		
		return false;
	
private static booleanisSpecial(java.lang.String filename)

		return filename.toUpperCase().startsWith(specialDir.toUpperCase());
	
public static voidmain(java.lang.String[] String_1darray1)

		try
		{
			ZipFile zip = new ZipFile("D:\\test\\AccessorTestEnterpriseBean.jar", "D:/test/zipOut");//NOI18N
			
			pr("" + zip);//NOI18N
                        zip.explode();
		}
		catch(ZipFileException e)
		{
			pr("ZipFileException: " + e);//NOI18N
		}
	
private static voidpr(java.lang.String s)

		System.out.println( s ); //NOI18N
	
public java.lang.StringtoString()

		String s = "Zip File Name: " + zipFilename + "\n";//NOI18N
		//s += "***** File Contents *********\n";//NOI18N
		//s += ContainerHelper.toOneString(getFileNames());
		
		return s;