java压缩文件或文件夹 博客分类: java开发 zipjava
程序员文章站
2024-03-24 16:53:28
...
/**
* @param inputFilePath
* 压缩 文件或文件夹的绝对路径
* @param outFilePath
* 压缩文件的输出路径
* */
public void zip(String inputFilePath, String outFilePath) throws Exception {
FileOutputStream output = null;
ZipOutputStream zipout = null;
try {
output = new FileOutputStream(outFilePath);
File inputFile = new File(inputFilePath);
if (!inputFile.exists()) {
_log.error(inputFilePath + " not found");
throw new FileNotFoundException(inputFilePath);
}
zipout = new ZipOutputStream(output);
executeZip(zipout, inputFile, "");
} catch (FileNotFoundException ex) {
_log.error(outFilePath + " not found |", ex);
throw new FileNotFoundException(outFilePath);
} finally {
closeStream(output, zipout);
}
}
/** 压缩 文件夹 */
private void executeZip(ZipOutputStream out, File file, String base)
throws Exception {
if (file.isDirectory()) {
File[] array = file.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < array.length; i++) {
executeZip(out, array[i], base + array[i].getName());
}
} else {
zipFile(out, file, base);
}
}
/** 压缩 文件 */
private void zipFile(ZipOutputStream outStream, File file, String base)
throws IOException {
putEntry(outStream, file, base);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
int c = 0;
byte[] bt = new byte[SIZE];
while ((c = inputStream.read(bt)) != -1) {
outStream.write(bt, 0, c);
}
} catch (FileNotFoundException ex) {
_log.error(file.getPath() + " not found |", ex);
throw ex;
} catch (IOException e) {
_log.error("IOException in zipFile |", e);
throw e;
} finally {
if (null != inputStream) {
inputStream.close();
}
}
}
private void putEntry(ZipOutputStream outStream, File file, String base)
throws IOException {
if (base.length() > 0) {
outStream.putNextEntry(new ZipEntry(base));
} else {
outStream.putNextEntry(new ZipEntry(file.getName()));
}
}
* @param inputFilePath
* 压缩 文件或文件夹的绝对路径
* @param outFilePath
* 压缩文件的输出路径
* */
public void zip(String inputFilePath, String outFilePath) throws Exception {
FileOutputStream output = null;
ZipOutputStream zipout = null;
try {
output = new FileOutputStream(outFilePath);
File inputFile = new File(inputFilePath);
if (!inputFile.exists()) {
_log.error(inputFilePath + " not found");
throw new FileNotFoundException(inputFilePath);
}
zipout = new ZipOutputStream(output);
executeZip(zipout, inputFile, "");
} catch (FileNotFoundException ex) {
_log.error(outFilePath + " not found |", ex);
throw new FileNotFoundException(outFilePath);
} finally {
closeStream(output, zipout);
}
}
/** 压缩 文件夹 */
private void executeZip(ZipOutputStream out, File file, String base)
throws Exception {
if (file.isDirectory()) {
File[] array = file.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < array.length; i++) {
executeZip(out, array[i], base + array[i].getName());
}
} else {
zipFile(out, file, base);
}
}
/** 压缩 文件 */
private void zipFile(ZipOutputStream outStream, File file, String base)
throws IOException {
putEntry(outStream, file, base);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
int c = 0;
byte[] bt = new byte[SIZE];
while ((c = inputStream.read(bt)) != -1) {
outStream.write(bt, 0, c);
}
} catch (FileNotFoundException ex) {
_log.error(file.getPath() + " not found |", ex);
throw ex;
} catch (IOException e) {
_log.error("IOException in zipFile |", e);
throw e;
} finally {
if (null != inputStream) {
inputStream.close();
}
}
}
private void putEntry(ZipOutputStream outStream, File file, String base)
throws IOException {
if (base.length() > 0) {
outStream.putNextEntry(new ZipEntry(base));
} else {
outStream.putNextEntry(new ZipEntry(file.getName()));
}
}
上一篇: JAVA ZIP压缩打包下载 博客分类: java 技术 zipjava
下一篇: Java压缩解压zip文件的中文文件名在Windows和Linux环境下乱码问题的解决方案 zipzipfilezipoutputstream
推荐阅读
-
java压缩文件或文件夹 博客分类: java开发 zipjava
-
java压缩zip文件乱码问题 博客分类: Java JavaZip压缩文件中文乱码乱码
-
使用GZip来压缩传输量 博客分类: java web开发 javaGZip
-
利用java解压.zip的压缩文件 博客分类: J2EE java解压zip
-
JAVA ZIP压缩打包下载 博客分类: java 技术 zipjava
-
服务器使用Gzip压缩JSON数据报“socket write error: Connection reset by peer.”错误 博客分类: java web开发 GzipJSONConnection reset by peer
-
服务器使用Gzip压缩数据,加快网络传输 博客分类: java web开发 javaGzip压缩数据
-
除了Guava,Java开发者还值得了解的5个谷歌类库 博客分类: 编程语言译文IT资源译文 Java类库
-
YYYY-MM-DD 的锅,服务端去背! 博客分类: java后端开发 JavaJava开发Java后端
-
win10安装java开发环境 博客分类: java系统