mysileng

常用链接

统计

最新评论

文件夹导入与导出

1package homeWork.first;
  
2
  3
import java.io.File;
  4
import java.io.FileInputStream;
  5
import java.io.FileNotFoundException;
  6
import java.io.FileOutputStream;
  7
import java.io.IOException;
  8
import java.util.zip.ZipEntry;
  9
import java.util.zip.ZipInputStream;
 10
import java.util.zip.ZipOutputStream;
 
11
 
12/** *//**
 13 * 压缩与解压类
 14 * 
 15 * 
@author mysileng
 16 * 
 17 
*/

 18
public class ZipDemo {
 
19
 
20    /** *//**
 21     * 压缩单个文件
 22     * 
 23     * 
@param fileIn
 24     * 
@param fileout
 25     
*/

 
26    public void singleFileZip(File fileIn, File fileout) {
 
27        FileInputStream in = null;
 
28        FileOutputStream out = null;
 
29        ZipOutputStream zip = null;
 
30        try {
 
31            in = new FileInputStream(fileIn);
 
32            out = new FileOutputStream(fileout);
 
33            zip = new ZipOutputStream(out);
 
34            byte[] b = new byte[1024];
 
35
 
36            ZipEntry entry = new ZipEntry(fileIn.getName());
 
37            zip.putNextEntry(entry);
 
38            while (in.read(b) != -1{
 
39                zip.write(b);
 
40            }

 
41        }
 catch (FileNotFoundException e) {
 
42            e.printStackTrace();
 
43        }
 catch (IOException e) {
 
44            e.printStackTrace();
 
45        }
 finally {
 
46            try {
 
47                zip.close();
 
48                out.close();
 
49                in.close();
 
50            }
 catch (IOException e) {
 
51                e.printStackTrace();
 
52            }

 
53
 
54        }

 
55
 
56    }

 
57
 
58    /** *//**
 59     * 压缩文件夹
 60     * 
 61     * 
@param befor文件夹压缩之前的路径
 62     * 
@param after文件夹压缩之后的路径
 63     
*/

 
64    public void mirsZip(String befor, String after) {
 
65        File fileIn = new File(befor);
 
66        File fileOut = new File(after);
 
67
 
68        FileOutputStream out = null;
 
69        ZipOutputStream zip = null;
 
70        String base = "";
 
71        try {
 
72            out = new FileOutputStream(fileOut);
 
73            zip = new ZipOutputStream(out);
 
74            zip(zip, fileIn, base);
 
75        }
 catch (FileNotFoundException e) {
 
76            e.printStackTrace();
 
77        }
 finally {
 
78            try {
 
79                zip.close();
 
80                out.close();
 
81            }
 catch (IOException e) {
 
82                e.printStackTrace();
 
83            }

 
84
 
85        }

 
86    }

 
87
 
88    private void zip(ZipOutputStream zip, File fileIn, String base) {
 
89        if (fileIn.isDirectory()) {
 
90            File[] list = fileIn.listFiles();
 
91            base = base + fileIn.getName() + "/";// 在压缩文件里建立文件夹
 92            ZipEntry entry = new ZipEntry(base);
 
93            try {
 
94                zip.putNextEntry(entry);
 
95            }
 catch (IOException e) {
 
96                e.printStackTrace();
 
97            }

 
98            for (int i = 0; i < list.length; i++{
 
99                zip(zip, list[i], base);
100            }

101        }
 else {
102            FileInputStream in = null;
103            try {
104                in = new FileInputStream(fileIn);
105                byte[] b = new byte[1024];
106                ZipEntry entry = new ZipEntry(base + fileIn.getName());
107                zip.putNextEntry(entry);
108                while (in.read(b) != -1{
109                    zip.write(b);
110                }

111            }
 catch (FileNotFoundException e) {
112                e.printStackTrace();
113            }
 catch (IOException e) {
114                e.printStackTrace();
115            }

116        }

117
118    }

119
120    /** *//**
121     * 解压
122     * 
123     * 
@param befor
124     * 
@param after
125     
*/

126    public void getZip(String befor, String after) {
127        File fileIn = new File(befor);
128        FileInputStream in = null;
129        FileOutputStream out = null;
130        ZipInputStream zip = null;
131
132        try {
133            in = new FileInputStream(fileIn);
134            zip = new ZipInputStream(in);
135            ZipEntry entry = null;
136
137            while ((entry = zip.getNextEntry()) != null{
138                if (entry.isDirectory()) {// 判断是文件夹还是文件
139                    new File(after + entry.getName()).mkdirs();// 如果是文件夹就建立
140                }
 else {
141                    out = new FileOutputStream(// 如果是文件就读出来
142                            new File(after + entry.getName()));
143                    byte[] b = new byte[1024];
144                    while (zip.read(b) != -1{
145                        out.write(b);
146                    }

147                    out.close();
148                }

149            }

150        }
 catch (FileNotFoundException e) {
151            e.printStackTrace();
152        }
 catch (IOException e) {
153            e.printStackTrace();
154        }
 finally {
155            try {
156                zip.close();
157                in.close();
158            }
 catch (IOException e) {
159                e.printStackTrace();
160            }

161        }

162
163    }

164}

165

posted on 2009-05-24 00:21 鑫龙 阅读(204) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: