java打包linux文件

    技术2022-06-23  45

    前几天写了文章介绍java文件夹的压缩(见http://zwllxs.javaeye.com/blog/836483),但是发现一个问题,在linux下,如果文件夹内的文件是中文名,那么压缩后,这些文件名全部成为了乱码而无法解压,要能支持linux压缩,有两点: 1:再压缩前,要设置linux模式, 如果是文件目录,则 Java代码 ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator")); zipEntry.setUnixMode(755);//解决linux乱码 ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator")); zipEntry.setUnixMode(755);//解决linux乱码 如果是文件,则 Java代码 ZipEntry zipEntry=new ZipEntry(base); zipEntry.setUnixMode(644);//解决linux乱码 ZipEntry zipEntry=new ZipEntry(base); zipEntry.setUnixMode(644);//解决linux乱码 然后在输出时强制设置编码: Java代码 ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFileName)); out.setEncoding("GBK");//解决linux乱码 ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFileName)); out.setEncoding("GBK");//解决linux乱码 全部代码如下: Java代码 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; /** * 文件夹压缩,支持win和linux * @author wlzhang * */ public class ZipUtil { /** * @param inputFileName * 输入一个文件夹 * @param zipFileName * 输出一个压缩文件夹,打包后文件名字 * @throws Exception */ public static OutputStream zip(String inputFileName, String zipFileName) throws Exception { return zip(zipFileName, new File(inputFileName)); } private static OutputStream zip(String zipFileName, File inputFile) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFileName)); out.setEncoding("GBK");//解决linux乱码 zip(out, inputFile, ""); out.close(); return out; } private static void zip(ZipOutputStream out, File f, String base) throws Exception { if (f.isDirectory()) { // 判断是否为目录 File[] fl = f.listFiles(); // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/")); // out.putNextEntry(new ZipEntry(base + "/")); ZipEntry zipEntry=new ZipEntry(base + System.getProperties().getProperty("file.separator")); zipEntry.setUnixMode(755);//解决linux乱码 out.putNextEntry(zipEntry); // base = base.length() == 0 ? "" : base + "/"; base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator"); for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } } else { // 压缩目录中的所有文件 // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base)); ZipEntry zipEntry=new ZipEntry(base); zipEntry.setUnixMode(644);//解决linux乱码 out.putNextEntry(zipEntry); FileInputStream in = new FileInputStream(f); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); } } } import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; /** * 文件夹压缩,支持win和linux * @author wlzhang * */ public class ZipUtil { /** * @param inputFileName * 输入一个文件夹 * @param zipFileName * 输出一个压缩文件夹,打包后文件名字 * @throws Exception */ public static OutputStream zip(String inputFileName, String zipFileName) throws Exception { return zip(zipFileName, new File(inputFileName)); } private static OutputStream zip(String zipFileName, File inputFile) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipFileName)); out.setEncoding("GBK");//解决linux乱码 zip(out, inputFile, ""); out.close(); return out; } private static void zip(ZipOutputStream out, File f, String base) throws Exception { if (f.isDirectory()) { // 判断是否为目录 File[] fl = f.listFiles(); // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/")); // out.putNextEntry(new ZipEntry(base + "/")); ZipEntry zipEntry=new ZipEntry(base + System.getProperties().getProperty("file.separator")); zipEntry.setUnixMode(755);//解决linux乱码 out.putNextEntry(zipEntry); // base = base.length() == 0 ? "" : base + "/"; base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator"); for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } } else { // 压缩目录中的所有文件 // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base)); ZipEntry zipEntry=new ZipEntry(base); zipEntry.setUnixMode(644);//解决linux乱码 out.putNextEntry(zipEntry); FileInputStream in = new FileInputStream(f); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); } } } 以上代码就同时支持win和linux


    最新回复(0)