java版的压缩

来源:互联网 发布:linux svn 删除库 编辑:程序博客网 时间:2024/05/21 08:53
package jframework.commons.util;
002 
003import java.io.BufferedInputStream;
004import java.io.BufferedOutputStream;
005import java.io.File;
006import java.io.FileInputStream;
007import java.io.FileNotFoundException;
008import java.io.FileOutputStream;
009import java.io.IOException;
010import java.io.InputStream;
011import java.io.OutputStream;
012import java.util.ArrayList;
013import java.util.Enumeration;
014import java.util.List;
015import org.apache.tools.zip.ZipEntry;
016import org.apache.tools.zip.ZipFile;
017import org.apache.tools.zip.ZipOutputStream;
018 
019/**
020* @author WYY
021* @description 压缩工具类
022*/
023public class ZipTool
024{
025    publicstatic final int BUFFER = 1024;// 缓存大小
026 
027    /**
028     * @description 压缩文件或目录(包含子目录压缩)
029     * @updated by WYY 2013年11月14日 下午10:11:34
030     * @param baseDir
031     *            待压缩目录或文件
032     * @param dest
033     *            要所后的文件名
034     */
035    publicstatic void zip(String baseDir, String dest)
036    {
037        File sourceFile =new File(baseDir);
038        File destFile =new File(dest);
039        ZipOutputStream zos =null;
040        try
041        {
042            if(sourceFile.isFile())
043            {// 单个文件压缩
044                zos =new ZipOutputStream(newFileOutputStream(destFile));
045                zipFile(baseDir, sourceFile, zos);
046            }
047            else
048            {// 文件夹压缩
049                List<File> fileList = getSubFiles(sourceFile);
050                zos =new ZipOutputStream(newFileOutputStream(destFile));
051                for(int i = 0; i < fileList.size(); i++)
052                {
053                    File subFile = (File) fileList.get(i);
054                    zipFile(baseDir, subFile, zos);
055                }
056            }
057        }
058        catch(IOException e)
059        {
060            e.getMessage();
061        }
062        finally
063        {
064            IOUtil.closeQuietly(zos);
065        }
066    }
067 
068    /**
069     * @description 压缩文件
070     * @updated by WYY 2013年11月14日 下午10:13:11
071     * @param baseDir
072     *            基本目录
073     * @param file
074     *            本次压缩的文件
075     * @param zos
076     * @throws IOException
077     * @throws FileNotFoundException
078     */
079    privatestatic void zipFile(String baseDir, File file, ZipOutputStream zos) throwsIOException,
080            FileNotFoundException
081    {
082        byte[] buf =new byte[BUFFER];
083        intreadLen = 0;
084        ZipEntry ze =new ZipEntry(getAbsFileName(baseDir, file));
085        ze.setSize(file.length());
086        ze.setTime(file.lastModified());
087        zos.putNextEntry(ze);
088        InputStream is =new BufferedInputStream(newFileInputStream(file));
089        while((readLen = is.read(buf, 0, BUFFER)) != -1)
090        {
091            zos.write(buf,0, readLen);
092        }
093        IOUtil.closeQuietly(is);
094    }
095 
096    /**
097     * 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径.
098     *
099     * @param baseDir
100     *            java.lang.String 根目录
101     * @param realFileName
102     *            java.io.File 实际的文件名
103     * @return 相对文件名
104     */
105    privatestatic String getAbsFileName(String baseDir, File realFileName)
106    {
107        File real = realFileName;
108        File base =new File(baseDir);
109        String ret = real.getName();
110        if(real.equals(base))// baseDir 为文件时,直接返回
111        {
112            returnret;
113        }
114        else
115        {
116            while(true)
117            {
118                real = real.getParentFile();
119                if(real == null)
120                    break;
121                if(real.equals(base))
122                    break;
123                else
124                    ret = real.getName() +"/" + ret;
125            }
126        }
127        returnret;
128    }
129 
130    /**
131     * 取得指定目录下的所有文件列表,包括子目录下的文件.
132     *
133     * @param baseDir
134     *            File 指定的目录
135     * @return 包含java.io.File的List
136     */
137    privatestatic List<File> getSubFiles(File baseDir)
138    {
139        List<File> ret =new ArrayList<File>();
140        File[] tmp = baseDir.listFiles();
141        for(int i = 0; i < tmp.length; i++)
142        {
143            if(tmp[i].isFile())
144                ret.add(tmp[i]);
145            if(tmp[i].isDirectory())
146                ret.addAll(getSubFiles(tmp[i]));
147        }
148        returnret;
149    }
150 
151    /**
152     * 解压缩功能. 将zipFile文件解压到zipDir目录下.
153     *
154     * @throws Exception
155     */
156    publicstatic void unzip(String zipDir, String zipFile)
157    {
158        ZipFile zfile =null;
159        InputStream is =null;
160        OutputStream os =null;
161        try
162        {
163            zfile =new ZipFile(zipFile);
164            Enumeration<?> zList = zfile.getEntries();
165            ZipEntry ze =null;
166            byte[] buf =new byte[1024];
167            while(zList.hasMoreElements())
168            {
169                ze = (ZipEntry) zList.nextElement();
170                if(ze.isDirectory())
171                {
172                    File f =new File(zipDir + ze.getName());
173                    f.mkdir();
174                    continue;
175                }
176                os =new BufferedOutputStream(newFileOutputStream(getRealFileName(zipDir, ze.getName())));
177                is =new BufferedInputStream(zfile.getInputStream(ze));
178                intreadLen = 0;
179                while((readLen = is.read(buf, 0,1024)) != -1)
180                {
181                    os.write(buf,0, readLen);
182                }
183                IOUtil.closeQuietly(is);
184                IOUtil.closeQuietly(os);
185            }
186            zfile.close();
187        }
188        catch(IOException e)
189        {
190            e.printStackTrace();
191        }
192        finally
193        {
194            IOUtil.closeQuietly(is);
195            IOUtil.closeQuietly(os);
196            try
197            {
198                if(null != zfile)
199                {
200                    zfile.close();
201                }
202            }
203            catch(IOException ex)
204            {
205                // ignore
206            }
207        }
208    }
209 
210    /**
211     * 给定根目录,返回一个相对路径所对应的实际文件名.
212     *
213     * @param baseDir
214     *            指定根目录
215     * @param absFileName
216     *            相对路径名,来自于ZipEntry中的name
217     * @return java.io.File 实际的文件
218     */
219    publicstatic File getRealFileName(String baseDir, String absFileName)
220    {
221        String[] dirs = absFileName.split("/");
222        File ret =new File(baseDir);
223        if(dirs.length > 1)
224        {
225            for(int i = 0; i < dirs.length -1; i++)
226            {
227                ret =new File(ret, dirs[i]);
228            }
229            if(!ret.exists())
230                ret.mkdirs();
231            ret =new File(ret, dirs[dirs.length -1]);
232            returnret;
233        }
234        ret =new File(ret, dirs[dirs.length -1]);
235        returnret;
236    }
237 
238    publicstatic void main(String[] args) throws Exception
239    {
240        // 测试
241        ZipTool.zip("E:/mdf","E:/mdf/22.zip");
242        ZipTool.unzip("E:/mdf","E:/mdf/22.zip");
243    }
244}

2. [代码][Java]代码    跳至[1][2][全屏预览]

view source
print?
01package jframework.commons.util;
02 
03import java.io.Closeable;
04import java.io.IOException;
05 
06/**
07* @author WYY
08* @description
09*/
10public class IOUtil
11{
12 
13    /**
14     * @description 关闭流
15     * @updated by WYY 2013年11月14日 下午10:10:25
16     * @param closeable
17     */
18    publicstatic void closeQuietly(Closeable closeable)
19    {
20        try
21        {
22            if(null != closeable)
23            {
24                closeable.close();
25            }
26        }
27        catch(IOException e)
28        {
29            // ignore
30        }
31    }
32}
原创粉丝点击