欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java代码ant、compress压缩文件,并保留文件的可执行权限

程序员文章站 2022-06-24 18:27:32
第一种,通过ant进行压缩,并授权0755import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;public static void write(File path, File zipFile) throws IOException {ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile))......

 

第一种,通过ant进行压缩,并授权0755

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
	public static void write(File path, File zipFile) throws IOException {
		ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile));
		zip.setEncoding("GBK");
		Util.write(path, path, zip);
		zip.close();
	}
 
	private static void write(File base, File path, ZipOutputStream zip) throws IOException {
		URI rel = base.toURI().relativize(path.toURI());
		if (path.isDirectory()) {
			ZipEntry entry = new ZipEntry(rel.getPath());
			entry.setUnixMode(0755);
			zip.putNextEntry(entry);
			zip.closeEntry();
			File[] files = path.listFiles();
			for (File file : files) {
				write(base, file, zip);
			}
		} else {
			ZipEntry entry = new ZipEntry(rel.getPath());
			entry.setUnixMode(644);
			zip.putNextEntry(entry);
			FileInputStream is = new FileInputStream(path);
			zip.write(IOUtils.toByteArray(is));
			is.close();
			zip.closeEntry();
		}
	}

第二种,通过compress进行压缩,并授权0755

public class JdCompressUtil {

    public static void toZipWithoutDir(String srcDir, String targetFile)
            throws RuntimeException, FileNotFoundException {

        OutputStream out = new FileOutputStream(targetFile);

        File sourceFile = new File(srcDir);

        List<File> files = Arrays.asList(sourceFile.listFiles());

        files = files.stream().filter(file -> file.isFile()).collect(Collectors.toList());

        toZip(files, out);
    }

    /**
     * 压缩成ZIP 方法2
     *
     * @param srcFiles 需要压缩的文件列表
     * @param out      压缩文件输出流
     * @throws RuntimeException 压缩失败会抛出运行时异常
     */
    public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipArchiveOutputStream zos = null;
        try {
            zos = new ZipArchiveOutputStream(out);
            for (File srcFile : srcFiles) {

                ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName());

                entry.setUnixMode(0755);

                zos.putArchiveEntry(entry);

                FileInputStream in = new FileInputStream(srcFile);

                BufferedInputStream ins = new BufferedInputStream(new FileInputStream(srcFile.getAbsolutePath()));
                IOUtils.copy(ins, zos);

                zos.closeArchiveEntry();

                in.close();
            }
            long end = System.currentTimeMillis();
            System.out.println("压缩完成,耗时:" + (end - start) + " ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

这里很重要的一点,开始测试的时候使用的授权码setUnixMode为755,这里死活不生效,改成0755就好了

 

本文地址:https://blog.csdn.net/philip502/article/details/109621282