【JAVA】圧縮ファイルおよび解凍
25839 ワード
以下はJAVAでファイル/フォルダをzipに圧縮し、解凍するコードです.
テストはMAC上の圧縮ソフトウェアよりも効果的で、より小さく抑えられています.コードは非常に明確で、2つの関数は、直接呼び出すことができます.次の例を示します.
test 1.zip圧縮パッケージとtestun解凍ソフトウェアが生成されます.とても便利です.
(zz:http://blog.sina.com.cn/s/blog_600ff0750100x61j.html)(例は自分の)
1 import java.io.File;
2 import java.io.FileInputStream;
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.Enumeration;
7 import java.util.zip.ZipEntry;
8 import java.util.zip.ZipFile;
9 import java.util.zip.ZipOutputStream;
10
11 public class FileZip {
12 public static void zip(String src, String dest) throws IOException {
13 // ZIP
14 ZipOutputStream out = null;
15 try {
16 File outFile = new File(dest);//
17 File fileOrDirectory = new File(src);//
18 out = new ZipOutputStream(new FileOutputStream(outFile));
19
20 // , false。
21 if (fileOrDirectory.isFile()) {
22 zipFileOrDirectory(out, fileOrDirectory, "");
23 } else {
24 // 。
25 File[] entries = fileOrDirectory.listFiles();
26 for (int i = 0; i < entries.length; i++) {
27 // , curPaths
28 zipFileOrDirectory(out, entries[i], "");
29 }
30 }
31 } catch (IOException ex) {
32 ex.printStackTrace();
33 } finally {
34 //
35 if (out != null) {
36 try {
37 out.close();
38 } catch (IOException ex) {
39 ex.printStackTrace();
40 }
41 }
42 }
43 }
44
45 private static void zipFileOrDirectory(ZipOutputStream out,
46 File fileOrDirectory, String curPath) throws IOException {
47 //
48 FileInputStream in = null;
49 try {
50 // , false。
51 if (!fileOrDirectory.isDirectory()) {
52 //
53 byte[] buffer = new byte[4096];
54 int bytes_read;
55 in = new FileInputStream(fileOrDirectory);
56 // ZIP
57 ZipEntry entry = new ZipEntry(curPath + fileOrDirectory.getName());
58 //
59 out.putNextEntry(entry);
60 while ((bytes_read = in.read(buffer)) != -1) {
61 out.write(buffer, 0, bytes_read);
62 }
63 out.closeEntry();
64 } else {
65 //
66 File[] entries = fileOrDirectory.listFiles();
67 for (int i = 0; i < entries.length; i++) {
68 // , curPaths
69 zipFileOrDirectory(out, entries[i], curPath + fileOrDirectory.getName() + "/");
70 }
71 }
72 } catch (IOException ex) {
73 ex.printStackTrace();
74 // throw ex;
75 } finally {
76 if (in != null) {
77 try {
78 in.close();
79 } catch (IOException ex) {
80 ex.printStackTrace();
81 }
82 }
83 }
84 }
85
86 @SuppressWarnings("unchecked")
87 public static void unzip(String zipFileName, String outputDirectory) throws IOException {
88 ZipFile zipFile = null;
89 try {
90 zipFile = new ZipFile(zipFileName);
91 Enumeration e = zipFile.entries();
92 ZipEntry zipEntry = null;
93 File dest = new File(outputDirectory);
94 dest.mkdirs();
95 while (e.hasMoreElements()) {
96 zipEntry = (ZipEntry) e.nextElement();
97 String entryName = zipEntry.getName();
98 InputStream in = null;
99 FileOutputStream out = null;
100 try {
101 if (zipEntry.isDirectory()) {
102 String name = zipEntry.getName();
103 name = name.substring(0, name.length() - 1);
104 File f = new File(outputDirectory + File.separator + name);
105 f.mkdirs();
106 } else {
107 int index = entryName.lastIndexOf("\\");
108 if (index != -1) {
109 File df = new File(outputDirectory + File.separator
110 + entryName.substring(0, index));
111 df.mkdirs();
112 }
113 index = entryName.lastIndexOf("/");
114 if (index != -1) {
115 File df = new File(outputDirectory + File.separator
116 + entryName.substring(0, index));
117 df.mkdirs();
118 }
119 File f = new File(outputDirectory + File.separator
120 + zipEntry.getName());
121 // f.createNewFile();
122 in = zipFile.getInputStream(zipEntry);
123 out = new FileOutputStream(f);
124 int c;
125 byte[] by = new byte[1024];
126 while ((c = in.read(by)) != -1) {
127 out.write(by, 0, c);
128 }
129 out.flush();
130 }
131 } catch (IOException ex) {
132 ex.printStackTrace();
133 throw new IOException(" :" + ex.toString());
134 } finally {
135 if (in != null) {
136 try {
137 in.close();
138 } catch (IOException ex) {
139 }
140 }
141 if (out != null) {
142 try {
143 out.close();
144 } catch (IOException ex) {
145 }
146 }
147 }
148 }
149 } catch (IOException ex) {
150 ex.printStackTrace();
151 throw new IOException(" :" + ex.toString());
152 } finally {
153 if (zipFile != null) {
154 try {
155 zipFile.close();
156 } catch (IOException ex) {
157 }
158 }
159 }
160 }
161 }
テストはMAC上の圧縮ソフトウェアよりも効果的で、より小さく抑えられています.コードは非常に明確で、2つの関数は、直接呼び出すことができます.次の例を示します.
1 import java.io.IOException;
2
3 public class Test {
4
5 public static void main(String[] args) {
6 // TODO Auto-generated method stub
7 FileZip zip = new FileZip();
8 try {
9 zip.zip("/Users/mac/Desktop/test", "/Users/mac/Desktop/test1.zip");
10 zip.unzip("/Users/mac/Desktop/test1.zip", "/Users/mac/Desktop/testun");
11 } catch (IOException e) {
12 // TODO Auto-generated catch block
13 e.printStackTrace();
14 }
15 }
16 }
test 1.zip圧縮パッケージとtestun解凍ソフトウェアが生成されます.とても便利です.
(zz:http://blog.sina.com.cn/s/blog_600ff0750100x61j.html)(例は自分の)