FileDocCategorySizeDatePackage
ZipFile.javaAPI DocGlassfish v2 API12308Fri May 04 22:23:16 BST 2007com.sun.enterprise.config.backup.util

ZipFile

public class ZipFile extends Object

Fields Summary
private static final int
BUFFER_SIZE
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)

		this(new File(zipFilename), new File(explodeDirName));
	
public ZipFile(File zipFile, File anExplodeDir)

		//checkZipFile(zipFile);
		BufferedInputStream bis = null;
		
		try
		{
			bis = new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE);
			ctor(bis, anExplodeDir);
		}
		catch(Throwable e)
		{
			if (bis != null) 
			{
				try 
				{
					bis.close();
				} 
				catch (Throwable thr) 
				{
					throw new ZipFileException(thr);
				}
			}
			
			throw new ZipFileException(e);
		}
	
public ZipFile(InputStream inStream, File anExplodeDir)

		ctor(inStream, anExplodeDir);
	
Methods Summary
private voidcheckExplodeDir()

		String explodeDirName = explodeDir.getPath();
		
		// just in case...
		explodeDir.mkdirs();
		
		insist(explodeDir.exists(),			"Target Directory doesn't exist: "		+ explodeDirName );//NOI18N
		insist(explodeDir.isDirectory(),	"Target Directory isn't a directory: "	+ explodeDirName );//NOI18N
		insist(explodeDir.canWrite(),		"Can't write to Target Directory: "		+ explodeDirName );//NOI18N
	
private voidcheckZipFile(java.io.File zipFile)

		insist(zipFile != null);
		
		String zipFileName = zipFile.getPath();

		insist( zipFile.exists(),		"zipFile (" + zipFileName + ") doesn't exist" );//NOI18N
		insist( !zipFile.isDirectory(), "zipFile (" + zipFileName + ") is actually a directory!" );//NOI18N
	
private voidctor(java.io.InputStream inStream, java.io.File anExplodeDir)

		insist(anExplodeDir != null);
		explodeDir = anExplodeDir;

		try
		{
			zipStream = new ZipInputStream(inStream);
			checkExplodeDir();
		}
		catch(Throwable t)
		{
                    if (zipStream != null) {
                        try {
                            zipStream.close();
                        } catch (Throwable thr) {
                        }
                    }
		    throw new ZipFileException(t.toString());
		}
	
private static java.util.ArrayListdoExplode(com.sun.enterprise.config.backup.util.ZipFile zf)
/******************************** Private ****************************** /

            ArrayList finalList = new ArrayList(50);
            ArrayList zipFileList = new ArrayList();
            ArrayList tmpList = null;
            ZipFile tmpZf = null;
            Iterator itr = null;
            String fileName = null;

            zipFileList.add(zf);
            while (zipFileList.size() > 0)
            {
                // get "last" jar to explode
                tmpZf = (ZipFile)zipFileList.remove(zipFileList.size() - 1);
                tmpList = tmpZf.explode();

                // traverse list of files
                itr = tmpList.iterator();
                while (itr.hasNext())
                {
                    fileName = (String)itr.next();
                    if ( ! fileName.endsWith(".jar") )
                    {
                        // add non-jar file to finalList
                        finalList.add(fileName);
                    }
                    else
                    {
                        // create ZipFile and add to zipFileList
                        File f = new File(tmpZf.explodeDir, fileName);
                        ZipFile newZf = new ZipFile(f, tmpZf.explodeDir);
                        zipFileList.add(newZf);
                    }

                    if (tmpZf != zf)  // don't remove first ZipFile
                    {
                        tmpZf.explodeDir.delete();
                    }
                }
            }
            return finalList;
        
public java.util.ArrayListexplode()

		files = new ArrayList();
		ZipInputStream zin = null;

		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 = null;
				
				if(isDirectory(filename))
				{
					// just a directory -- make it and move on...
					fullpath = new File(explodeDir, filename.substring(0, filename.length() - 1));
					
					// bnevins.  FAT BUG FIX!!!  This was mkdir() not mkdirs()
					// this should be back-ported to appserv-commons
					
					fullpath.mkdirs();
					continue;
				}
					
				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
				}

				BufferedOutputStream os = new BufferedOutputStream(getOutputStream(filename), BUFFER_SIZE);

				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
				files.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 ZipFile"  + " to " + explodeDir.getPath());//NOI18N
		return files;
	
public java.util.ArrayListgetFileList()

		return files;
	
private java.io.FileOutputStreamgetOutputStream(java.lang.String filename)

		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("filename: " + f.getPath() + "  " + e);
		}
		catch(IOException e)
		{
			throw new ZipFileException(e);
		}
	
private static voidinsist(java.lang.String s)

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

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

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

		if( !b )
			throw new ZipFileException( mesg );
		else
			return;
	
private booleanisDirectory(java.lang.String s)

		char c = s.charAt(s.length() - 1);
		
		return c== '/" || c == '\\";
	
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());
	
private static voidpr(java.lang.String s)

		System.out.println( s );