ZIP,是一个计算机文件的压缩的算法,原名Deflate(真空),发明者为菲利普·卡兹(Phil Katz)),他于1989年1月公布了该格式的资料。ZIP通常使用后缀名“.zip”,它的MIME格式为 application/zip 。目前,ZIP格式属于几种主流的压缩格式之一,其竞争者包括RAR格式以及开放源码的7-Zip格式。从性能上比较,RAR格式较ZIP格式压缩率较高,但是它的压缩时间远远高于Zip。而7-Zip(7z)由于提供了免费的压缩工具而逐渐在更多的领域得到应用。Java中对其也提供了支持,我们可以很方便的对zip文件进行操作。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
* 文件压缩工具
*
* @author jianggujin
*
*/
public class ZipUtils
{
/**
* 压缩文件
*
* @param zip
* 压缩后文件
* @param path
* 文件夹,为空则不添加文件夹
* @param srcFiles
* 压缩源文件
* @throws IOException
*/
public void zipFiles(File zip, String path, File... srcFiles)
throws IOException
{
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip));
zipFiles(out, path, srcFiles);
out.close();
}
/**
* 压缩文件
*
* @param out
* 压缩输出流
* @param path
* 文件夹,为空则不添加文件夹
* @param srcFiles
* 压缩源文件
* @throws IOException
*/
public void zipFiles(ZipOutputStream out, String path, File... srcFiles)
throws IOException
{
if (path == null)
{
path = "";
}
path = path.replaceAll("\\*", "/");
if (path.length() > 0 && !path.endsWith("/"))
{
path += "/";
}
byte[] buf = new byte[1024];
for (int i = 0; i < srcFiles.length; i++)
{
if (srcFiles[i].isDirectory())
{
File[] files = srcFiles[i].listFiles();
String srcPath = srcFiles[i].getName();
srcPath = srcPath.replaceAll("\\*", "/");
if (!srcPath.endsWith("/"))
{
srcPath += "/";
}
out.putNextEntry(new ZipEntry(path + srcPath));
zipFiles(out, path + srcPath, files);
}
else
{
FileInputStream in = new FileInputStream(srcFiles[i]);
out.putNextEntry(new ZipEntry(path + srcFiles[i].getName()));
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
}
}
/**
* 解压到指定目录
*
* @param zipPath
* @param descDir
* @throws IOException
*/
public void unZipFiles(String zipPath, String descDir) throws IOException
{
unZipFiles(new File(zipPath), new File(descDir), null);
}
/**
* 解压到指定目录
*
* @param zipFile
* @param pathFile
* @throws IOException
*/
public void unZipFiles(File zipFile, File pathFile) throws IOException
{
unZipFiles(zipFile, pathFile, null);
}
/**
* 解压到指定目录
*
* @param zipFile
* @param pathFile
* @param zipList
* 压缩文件内路径集合
* @throws IOException
*/
@SuppressWarnings("rawtypes")
public void unZipFiles(File zipFile, File pathFile, List<String> zipList)
throws IOException
{
if (!pathFile.exists())
{
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(zipFile);
for (Enumeration entries = zip.entries(); entries.hasMoreElements();)
{
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = zipEntryName.replaceAll("\\*", "/");
int index = outPath.lastIndexOf('/');
if (index != -1)
{
File file = new File(pathFile, outPath.substring(0,
outPath.lastIndexOf('/')));
if (!file.exists())
{
file.mkdirs();
}
}
File realFile = new File(pathFile, outPath);
if (zipList != null)
{
zipList.add(outPath);
}
if (realFile.isDirectory())
{
continue;
}
OutputStream out = new FileOutputStream(realFile);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0)
{
out.write(buf1, 0, len);
}
in.close();
out.close();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173