1 18 19 package sync4j.syncclient.common; 20 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.zip.ZipEntry ; 27 import java.util.zip.ZipInputStream ; 28 29 38 39 public class ZipTools { 40 41 42 50 public static void extract(String workingDirectory, byte[] zipFile) 51 throws Exception { 52 53 ByteArrayInputStream byteStream = new ByteArrayInputStream (zipFile); 54 ByteArrayOutputStream out = new ByteArrayOutputStream (); 55 56 ZipInputStream zipStream = new ZipInputStream (byteStream); 57 ZipEntry zipEntry = null; 58 59 String nameZipEntry = null; 60 61 byte[] contentZipEntry = null; 62 boolean isDirectory = false; 63 64 int indexFileSeparator = -1; 65 String directory = null; 66 String fileName = null; 67 68 while ( (zipEntry = zipStream.getNextEntry()) != null ) { 69 nameZipEntry = workingDirectory + File.separator + zipEntry.getName(); 70 71 isDirectory = zipEntry.isDirectory(); 72 73 if (isDirectory) { 74 File file = new File (nameZipEntry); 75 file.mkdirs(); 76 } else { 77 78 byte[] buf = new byte[1024]; 80 int c = 0; 81 82 while ( (c = zipStream.read(buf)) != -1) { 83 out.write(buf, 0, c); 84 } 85 86 indexFileSeparator = nameZipEntry.lastIndexOf(File.separator); 87 directory = nameZipEntry.substring(0, indexFileSeparator); 88 fileName = nameZipEntry.substring(indexFileSeparator+1,nameZipEntry.length()); 89 90 FileSystemTools.createFile(directory, fileName, out); 91 92 out.reset(); 93 zipStream.closeEntry(); 94 } 95 } 96 zipStream.close(); 97 byteStream.close(); 98 } 99 100 106 public static void verifyZip(byte[] zipFile) throws IOException { 107 108 boolean isValidZipFile = true; 109 110 ByteArrayInputStream bStream = new ByteArrayInputStream (zipFile); 111 112 ZipInputStream zipStream = new ZipInputStream (bStream); 113 114 ZipEntry zipEntry = null; 115 116 String nameZipEntry = null; 117 int dimZipEntry = 0; 118 byte[] contentZipEntry = null; 119 boolean isDirectory = false; 120 121 while ( (zipEntry = zipStream.getNextEntry()) != null ) { 122 123 isDirectory = zipEntry.isDirectory(); 124 125 if (!isDirectory) { 126 int c = -1; 128 byte[] buf = new byte[1024]; 129 while ( (c = zipStream.read(buf)) != -1) { 130 dimZipEntry = dimZipEntry + c; 131 } 132 dimZipEntry = 0; 133 zipStream.closeEntry(); 134 } 135 } 136 zipStream.close(); 137 bStream.close(); 138 } 139 } | Popular Tags |