package com.topsoft.websites.utils; |
002 |
003 | import java.io.*; |
004 | import java.util.logging.Logger; |
005 | import java.util.zip.*; |
006 |
007 | /** |
008 | * Created by surperman on 14-3-10. |
009 | */ |
010 | public class ZipUtil { |
011 | private final static Logger logger = Logger.getLogger(ZipUtil. class .getName()); |
012 | private static final int BUFFER = 1024 * 10 ; |
013 | /** |
014 | * 将指定目录压缩到和该目录同名的zip文件,自定义压缩路径 |
015 | * |
016 | * @param sourceFilePath 目标文件路径 |
017 | * @param zipFilePath 指定zip文件路径 |
018 | * @return |
019 | */ |
020 | public static boolean zip(String sourceFilePath, String zipFilePath) { |
021 | boolean result = false ; |
022 | File source = new File(sourceFilePath); |
023 | if (!source.exists()) { |
024 | logger.info(sourceFilePath + " doesn't exist." ); |
025 | return result; |
026 | } |
027 | if (!source.isDirectory()) { |
028 | logger.info(sourceFilePath + " is not a directory." ); |
029 | return result; |
030 | } |
031 | File zipFile = new File(zipFilePath + File.separator + source.getName() + ".zip" ); |
032 | if (zipFile.exists()) { |
033 | logger.info(zipFile.getName() + " is already exist." ); |
034 | return result; |
035 | } else { |
036 | if (!zipFile.getParentFile().exists()) { |
037 | if (!zipFile.getParentFile().mkdirs()) { |
038 | logger.info( "cann't create file " + zipFile.getName()); |
039 | return result; |
040 | } |
041 | } |
042 | } |
043 | logger.info( "creating zip file..." ); |
044 | FileOutputStream dest = null ; |
045 | ZipOutputStream out = null ; |
046 | try { |
047 | dest = new FileOutputStream(zipFile); |
048 | CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32()); |
049 | out = new ZipOutputStream( new BufferedOutputStream(checksum)); |
050 | out.setMethod(ZipOutputStream.DEFLATED); |
051 | compress(source, out, source.getName()); |
052 | result = true ; |
053 | } catch (FileNotFoundException e) { |
054 | e.printStackTrace(); |
055 | } finally { |
056 | if (out != null ) { |
057 | try { |
058 | out.closeEntry(); |
059 | } catch (IOException e) { |
060 | e.printStackTrace(); |
061 | } |
062 | try { |
063 | out.close(); |
064 | } catch (IOException e) { |
065 | e.printStackTrace(); |
066 | } |
067 | } |
068 | } |
069 | if (result) { |
070 | logger.info( "done." ); |
071 | } else { |
072 | logger.info( "fail." ); |
073 | } |
074 | return result; |
075 | } |
076 |
077 | private static void compress(File file, ZipOutputStream out, String mainFileName) { |
078 | int index = file.getAbsolutePath().indexOf(mainFileName); |
079 | String entryName = file.getAbsolutePath().substring(index); |
080 | System.out.println(entryName); |
081 | if (file.isFile()) { |
082 | FileInputStream fi = null ; |
083 | BufferedInputStream origin = null ; |
084 | try { |
085 | fi = new FileInputStream(file); |
086 | origin = new BufferedInputStream(fi, BUFFER); |
087 | ZipEntry entry = new ZipEntry(entryName); |
088 | out.putNextEntry(entry); |
089 | byte [] data = new byte [BUFFER]; |
090 | int count; |
091 | while ((count = origin.read(data, 0 , BUFFER)) != - 1 ) { |
092 | out.write(data, 0 , count); |
093 | } |
094 | } catch (FileNotFoundException e) { |
095 | e.printStackTrace(); |
096 | } catch (IOException e) { |
097 | e.printStackTrace(); |
098 | } finally { |
099 | if (origin != null ) { |
100 | try { |
101 | origin.close(); |
102 | } catch (IOException e) { |
103 | e.printStackTrace(); |
104 | } |
105 | } |
106 | } |
107 | } else if (file.isDirectory()) { |
108 | try { |
109 | out.putNextEntry( new ZipEntry(entryName+File.separator)); |
110 | } catch (IOException e) { |
111 | e.printStackTrace(); |
112 | } |
113 | File[] fs = file.listFiles(); |
114 | if (fs != null && fs.length > 0 ) { |
115 | for (File f : fs) { |
116 | compress(f, out, mainFileName); |
117 | } |
118 | } |
119 | } |
120 | } |
121 |
122 | /** |
123 | * 将zip文件解压到指定的目录,该zip文件必须是使用该类的zip方法压缩的文件 |
124 | * |
125 | * @param zipFile |
126 | * @param destPath |
127 | * @return |
128 | */ |
129 | public static boolean unzip(File zipFile, String destPath) { |
130 | boolean result = false ; |
131 | if (!zipFile.exists()) { |
132 | logger.info(zipFile.getName() + " doesn't exist." ); |
133 | return result; |
134 | } |
135 | File target = new File(destPath); |
136 | if (!target.exists()) { |
137 | if (!target.mkdirs()) { |
138 | logger.info( "cann't create file " + target.getName()); |
139 | return result; |
140 | } |
141 | } |
142 | String mainFileName = zipFile.getName().replace( ".zip" , "" ); |
143 | File targetFile = new File(destPath + File.separator + mainFileName); |
144 | if (targetFile.exists()) { |
145 | logger.info(targetFile.getName() + " already exist." ); |
146 | return result; |
147 | } |
148 | ZipInputStream zis = null ; |
149 | logger.info( "start unzip file ..." ); |
150 | try { |
151 | FileInputStream fis = new FileInputStream(zipFile); |
152 | CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32()); |
153 | zis = new ZipInputStream( new BufferedInputStream(checksum)); |
154 | ZipEntry entry; |
155 | while ((entry = zis.getNextEntry()) != null ) { |
156 | int count; |
157 | byte data[] = new byte [BUFFER]; |
158 | String entryName = entry.getName(); |
159 | int index = entryName.indexOf(mainFileName); |
160 | String newEntryName = destPath + File.separator + entryName.substring(index); |
161 | System.out.println(newEntryName); |
162 | File f = new File(newEntryName); |
163 | if (newEntryName.endsWith(File.separator)){ |
164 | if (!f.exists()){ |
165 | if (!f.mkdirs()) { |
166 | throw new RuntimeException( "can't create directory " + f.getName()); |
167 | } |
168 | } |
169 | } else { |
170 | File temp=f.getParentFile(); |
171 | if (!temp.exists()) { |
172 | if (!temp.mkdirs()) { |
173 | throw new RuntimeException( "create file " + temp.getName() + " fail" ); |
174 | } |
175 | } |
176 | FileOutputStream fos = new FileOutputStream(f); |
177 | BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); |
178 | while ((count = zis.read(data, 0 , BUFFER)) != - 1 ) { |
179 | dest.write(data, 0 , count); |
180 | } |
181 | dest.flush(); |
182 | dest.close(); |
183 | } |
184 | } |
185 | result = true ; |
186 | } catch (FileNotFoundException e) { |
187 | e.printStackTrace(); |
188 | } catch (IOException e) { |
189 | e.printStackTrace(); |
190 | } finally { |
191 | if (zis != null ) { |
192 | try { |
193 | zis.close(); |
194 | } catch (IOException e) { |
195 | e.printStackTrace(); |
196 | } |
197 | } |
198 | } |
199 | if (result) { |
200 | logger.info( "done." ); |
201 | } else { |
202 | logger.info( "fail." ); |
203 | } |
204 | return result; |
205 | } |
206 |
207 | public static void main(String[] args) throws IOException { |
208 | // String path="D:\\local_dev_view\\Dev_TopWebSites_V2.0\\Cobweb\\WebSites\\frontsites\\docroot\\anhuigs"; |
209 | //ZipUtil.zip(path, "d:/temp"); |
210 | File zipFile = new File( "D:/temp/anhuigs.zip" ); |
211 | ZipUtil.unzip(zipFile, "d:/temp" ); |
212 | } |
213 | } |