You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
145 lines
4.5 KiB
Java
145 lines
4.5 KiB
Java
2 years ago
|
package com.api.sfj.DA.util;//package com.api.sfj.DA.util;
|
||
|
|
||
|
import org.apache.tools.zip.ZipEntry;
|
||
|
import org.apache.tools.zip.ZipOutputStream;
|
||
|
|
||
|
import java.io.BufferedInputStream;
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.FileOutputStream;
|
||
|
import java.util.Properties;
|
||
|
import java.util.zip.CRC32;
|
||
|
import java.util.zip.CheckedOutputStream;
|
||
|
|
||
|
/**
|
||
|
* 文件压缩
|
||
|
* zip
|
||
|
*/
|
||
|
public class ZipFile {
|
||
|
// public static void main(String[] args)
|
||
|
// throws Exception {
|
||
|
// File f = new File("D:\\WEAVER\\20200928144159323");
|
||
|
// ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
|
||
|
// "d:\\WEAVER\\20200928144159323.zip"));
|
||
|
// zip(out, f, null);
|
||
|
// System.out.println("zip done");
|
||
|
// out.close();
|
||
|
// }
|
||
|
public static void zip(ZipOutputStream out, File f, String base)
|
||
|
throws Exception {
|
||
|
System.out.println("zipping " + f.getAbsolutePath());
|
||
|
if (f.isDirectory()) {
|
||
|
File[] fc = f.listFiles();
|
||
|
if (base != null)
|
||
|
out.putNextEntry(new ZipEntry(base + "/"));
|
||
|
base = base == null ? "" : base + "/";
|
||
|
for (int i = 0; i < fc.length; i++) {
|
||
|
zip(out, fc[i], base + fc[i].getName());
|
||
|
}
|
||
|
} else {
|
||
|
out.putNextEntry(new ZipEntry(base));
|
||
|
FileInputStream in = new FileInputStream(f);
|
||
|
int b;
|
||
|
while ((b = in.read()) != -1)
|
||
|
out.write(b);
|
||
|
in.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* linux压缩文件
|
||
|
* @param srcPathName
|
||
|
* @param zipFileName
|
||
|
*/
|
||
|
public static void zipu(String srcPathName, String zipFileName)
|
||
|
{
|
||
|
File file = new File(srcPathName);
|
||
|
File zipFile = new File(zipFileName);
|
||
|
if (!file.exists()) throw new RuntimeException(srcPathName + "不存在!");
|
||
|
try
|
||
|
{
|
||
|
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
|
||
|
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
|
||
|
ZipOutputStream out = new ZipOutputStream(cos);
|
||
|
Properties pro=System.getProperties();
|
||
|
String osName=pro.getProperty("os.name");
|
||
|
if("Linux".equals(osName)||"linux".equals(osName)){
|
||
|
out.setEncoding("GBK");//设置文件名编码方式
|
||
|
}else{
|
||
|
out.setEncoding(System.getProperty("sun.jnu.encoding"));//设置文件名编码方式
|
||
|
}
|
||
|
|
||
|
String basedir = "";
|
||
|
compress(file, out, basedir);
|
||
|
out.close();
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
throw new RuntimeException(e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* inputFileName 输入一个文件夹
|
||
|
* zipFileName 输出一个压缩文件夹
|
||
|
*/
|
||
|
private static void compress(File file, ZipOutputStream out, String basedir)
|
||
|
{
|
||
|
/* 判断是目录还是文件 */
|
||
|
if (file.isDirectory())
|
||
|
{
|
||
|
// System.out.println("压缩:" + basedir + file.getName());
|
||
|
compressDirectory(file, out, basedir);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// System.out.println("压缩:" + basedir + file.getName());
|
||
|
compressFile(file, out, basedir);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** 压缩一个目录 */
|
||
|
private static void compressDirectory(File dir, ZipOutputStream out, String basedir)
|
||
|
{
|
||
|
if (!dir.exists()) return;
|
||
|
|
||
|
File[] files = dir.listFiles();
|
||
|
for (int i = 0; i < files.length; i++)
|
||
|
{
|
||
|
/* 递归 */
|
||
|
compress(files[i], out, basedir + dir.getName() + "/");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/** 压缩一个文件 */
|
||
|
private static void compressFile(File file, ZipOutputStream out, String basedir)
|
||
|
{
|
||
|
if (!file.exists())
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
try
|
||
|
{
|
||
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
||
|
ZipEntry entry = new ZipEntry(basedir + file.getName());
|
||
|
Properties pro=System.getProperties();
|
||
|
String osName=pro.getProperty("os.name");
|
||
|
if("Linux".equals(osName)||"linux".equals(osName)) {
|
||
|
entry.setUnixMode(644);
|
||
|
}
|
||
|
out.putNextEntry(entry);
|
||
|
int count;
|
||
|
byte data[] = new byte[8192];
|
||
|
while ((count = bis.read(data, 0, 8192)) != -1)
|
||
|
{
|
||
|
out.write(data, 0, count);
|
||
|
}
|
||
|
bis.close();
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
throw new RuntimeException(e);
|
||
|
}
|
||
|
}
|
||
|
}
|