java zip压缩文件问题

1、linux 判断路径file.isDirectory()返回false
在这里插入图片描述

  1. file.listFiles()总是返回null,报空指针异常,网上有说文件权限问题,查看路径看到前面是正斜杠,后面的是反斜杠,复制到ftp中也无法正确访问:
    /home/tmn/aiap/uploadPath\upload\CS0901

尝试将所有反斜杠替换成正斜杠,不再报错
filePath = filePath .replaceAll("\\\\","/");

在这里插入图片描述


3. zip压缩文件乱码问题: 这里ZipEntry和ZipOutputStream都引的java.util里的包
// 设置编码格式,解决linux压缩乱码问题
ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compresspath + “.zip”)),Charset.forName(“GBK”));

压缩文件工具类完整代码:

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

/**
 * 
 * @ClassName: CompressDirUtil
 * 
 * @Description: 压缩文件工具类
 * 
 * @author
 * 
 * @date
 * 
 * @version V1.0
 * 
 */

public class CompressDirUtil {

	/**
	 * 
	 * @Title: compressAllFileZip
	 * 
	 * @Description: 传递文件路径压缩文件,传递文件夹路径压缩文件夹,注:空的文件夹不会出现在压缩包内
	 * 
	 * @param @param compresspath 需要压缩的文件夹的目录
	 * 
	 * @return void 返回类型
	 * 
	 * @throws
	 * 
	 */

	public static ZipOutputStream compressFileToZip(String compresspath) {

		try {

			ZipOutputStream zipOutput = null;
			// 将所有反斜杠替换成正斜杠,否则影响linux文件目录判断
			compresspath = compresspath.replaceAll("\\\\","/");
			File file = new File(compresspath);
			System.out.println("文件路径:  " + compresspath);
			System.out.println("file.isDirectory() ===========================" + file.isDirectory());
			if (file.isDirectory()) {
				// 设置编码格式,解决linux压缩乱码问题
				zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(compresspath + ".zip")),Charset.forName("GBK"));


				compressZip(zipOutput, file, ""); // 递归压缩文件夹,最后一个参数传""压缩包就不会有当前文件夹;传file.getName(),则有当前文件夹;

			} else {

				zipOutput = new ZipOutputStream(new BufferedOutputStream(
						new FileOutputStream(compresspath.substring(0, compresspath.lastIndexOf(".")) + ".zip")));

				zipOFile(zipOutput, file); // 压缩单个文件

			}
			zipOutput.flush();
			zipOutput.close();
			return zipOutput;

		} catch (Exception e) {

			e.printStackTrace();

		}
		return null;

	}

	/**
	 * 
	 * @Title: compressZip
	 * 
	 * @Description: 子文件夹中可能还有文件夹,进行递归
	 * 
	 * @param @param  zipOutput
	 * 
	 * @param @param  file
	 * 
	 * @param @param  suffixpath
	 * 
	 * @param @throws IOException
	 * 
	 * @return void 返回类型
	 * 
	 * @throws
	 * 
	 */

	private static void compressZip(ZipOutputStream zipOutput, File file, String suffixpath) {

		File[] listFiles = file.listFiles();// 列出所有的文件

		for (File fi : listFiles) {

			if (fi.isDirectory()) {

				if (suffixpath.equals("")) {

					compressZip(zipOutput, fi, fi.getName());

				} else {

					compressZip(zipOutput, fi, suffixpath + File.separator + fi.getName());

				}

			} else {

				zip(zipOutput, fi, suffixpath);

			}

		}

	}

	/**
	 * 
	 * @Title: zip
	 * 
	 * @Description: 压缩的具体操作
	 * 
	 * @param @param zipOutput
	 * 
	 * @param @param file 文件
	 * 
	 * @param @param suffixpath 文件夹拼接路径
	 * 
	 * @return void 返回类型
	 * 
	 * @throws
	 * 
	 */

	public static void zip(ZipOutputStream zipOutput, File file, String suffixpath) {

		try {

			ZipEntry zEntry = null;

			if (suffixpath.equals("")) {

				zEntry = new ZipEntry(file.getName());

			} else {

				zEntry = new ZipEntry(suffixpath + File.separator + file.getName());

			}

			zipOutput.putNextEntry(zEntry);

			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

			byte[] buffer = new byte[1024];

			int read = 0;

			while ((read = bis.read(buffer)) != -1) {

				zipOutput.write(buffer, 0, read);

			}

			bis.close();

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

	/**
	 * 
	 * @Title: zip
	 * 
	 * @Description: 压缩单个文件
	 * 
	 * @param @param zipOutput
	 * 
	 * @param @param file 文件
	 * 
	 * @return void 返回类型
	 * 
	 * @throws
	 * 
	 */

	public static void zipOFile(ZipOutputStream zipOutput, File file) {

		try {

			ZipEntry zEntry = new ZipEntry(file.getName());

			zipOutput.putNextEntry(zEntry);

			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

			byte[] buffer = new byte[1024];

			int read = 0;

			while ((read = bis.read(buffer)) != -1) {

				zipOutput.write(buffer, 0, read);

			}

			bis.close();

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

	static String compresspath = "D:\\aiap\\uploadPath\\upload\\信息服务设施设备基础审计"; // 需要压缩的文件夹的目录

	public static void main(String[] args) {

		/*
		 * boolean bl = compressFileToZip(compresspath); // 压缩文件
		 * 
		 * if (bl) {
		 * 
		 * System.out.println("压缩成功");
		 * 
		 * }
		 */
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值