Archive for August, 2012

Exception ZipFile

Thursday, August 23rd, 2012

He works professionally with Java he has 7 years and he possesss 4 years of experience in the development of systems for the IBM of the United States, used the world-wide level. He acts more than has 2 years with courses and training of professionals in great companies. It possesss the certifications of Sun SCJP, SCJD, SCWCD, SCBCD, SCEA, IBM SOUNDS and ITIL Foundation. Code-source used in the article: package zip; import java.io.Fi le; import java.io.Fi leInputStream; import java.io.Fi leOutputStream; import java.io.IO Exception; import java.io.In putStream; import java.io.Ou tputStream; import java.util. Enumeration; import java.util. Stack; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class ZipHelper public void zip (File files, File outputFile) throws IOException if (files! = null & & files.length > 0) ZipOutputStream out = new ZipOutputStream (new FileOutputStream (outputFile)); Stack parentDirs = new Stack (); zipFiles (parentDirs, files, out); out.close (); private void zipFiles (Stack parentDirs, File files, ZipOutputStream out) throws IOException byte buf = new byte 1024; i will be (int = 0; i 0) out.write (buf, 0, len); out.closeEntry (); in.close (); public void unzip (File zipFile, File to dir) throws IOException ZipFile zip = null; File archive = null; InputStream is = null; OutputStream = null; byte buffer = new byte 1024; try // creates informed directory, in case that if does not exist (! dir.exists ()) dir.mkdirs (); if (! dir.exists () dir.isDirectory ()) throw new IOException (' ' The directory ' ' + dir.getName () + ' ' vlido&#039 is not a directory; '); zip = new ZipFile (zipFile); Enumeration = zip.entries (); while (e.hasMoreElements ()) Entered ZipEntry = (ZipEntry) e.nextElement (); archive = new File (to dir, entrada.getName ()); // will be inexistent directory, creates the structure and polishes pra next entered if (entrada.isDirectory () & & arquivo.exists ()) arquivo.mkdirs (); it continues; // if the structure of directories does not exist, creates if (! arquivo.getParentFile () .exists ()) arquivo.getParentFile () .mkdirs (); try // reads the archive of zip and records in record is = zip.getInputStream (entered); = new FileOutputStream (archive); int bytesLidos = 0; if (is == null) throw new ZipException (' ' Error when reading the entrance of zip: ' ' + entrada.getName ()); while ((bytesLidos = is.read (buffer)) > 0) os.write (buffer, 0, bytesLidos); finally if (is! = null) try is.close (); catch (former Exception) if (! = null) try os.close (); catch (former Exception) finally if (zip! = null) try zip.close (); catch (Exception e)