文件的压缩和解压
程序员文章站
2024-03-13 23:43:52
...
//文件的压缩和解压
public class GzipUtils {
public static void zip(String sourcePath, String outPath) throws Exception {
zip(new File(sourcePath), new File(outPath));
}
public static void zip(File source, File out) throws Exception {
zip(new FileInputStream(source), new FileOutputStream(out));
}
public static void zip(InputStream is, OutputStream os) throws Exception {
GZIPOutputStream gos = null;
try {
gos = new GZIPOutputStream(os);
int len;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
gos.write(buffer, 0, len);
}
} finally {
StreamUtils.closeStream(gos);
StreamUtils.closeStream(os);
StreamUtils.closeStream(is);
}
}
public static void unZip(String zipPath, String unzipPath)
throws Exception {
unZip(new File(zipPath), new File(unzipPath));
}
public static void unZip(File zipFile, File unzipFile) throws Exception {
unZip(new FileInputStream(zipFile), new FileOutputStream(unzipFile));
}
public static void unZip(InputStream is, OutputStream os) throws Exception {
GZIPInputStream gis = null;
try {
gis = new GZIPInputStream(is);
int len;
byte[] buffer = new byte[1024];
while ((len = gis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} finally {
StreamUtils.closeStream(os);
StreamUtils.closeStream(gis);
StreamUtils.closeStream(is);
}
}
}