首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

java封装linux文件

2012-08-28 
java打包linux文件 .01.前几天写了文章介绍java文件夹的压缩(见http://zwllxs.javaeye.com/blog/836483),

java打包linux文件 .

01.   前几天写了文章介绍java文件夹的压缩(见http://zwllxs.javaeye.com/blog/836483),但是发现一个问题,在linux下,如果文件夹内的文件是中文名,那么压缩后,这些文件名全部成为了乱码而无法解压,要能支持linux压缩,有两点:    02.1:再压缩前,要设置linux模式,   03.  如果是文件目录,则   04.Java代码   05.ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator"));     06.zipEntry.setUnixMode(755);//解决linux乱码     07.  08.ZipEntry zipEntry=new ZipEntry(basePath + System.getProperties().getProperty("file.separator"));  09.zipEntry.setUnixMode(755);//解决linux乱码   10.如果是文件,则   11.Java代码   12.ZipEntry zipEntry=new ZipEntry(base);     13. zipEntry.setUnixMode(644);//解决linux乱码     14.  15.ZipEntry zipEntry=new ZipEntry(base);  16. zipEntry.setUnixMode(644);//解决linux乱码   17.  18.然后在输出时强制设置编码:   19.Java代码   20.ZipOutputStream out = new ZipOutputStream(new FileOutputStream(     21.               zipFileName));     22.       out.setEncoding("GBK");//解决linux乱码     23.  24. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(  25.                zipFileName));  26.        out.setEncoding("GBK");//解决linux乱码   27.  28.全部代码如下:   29.  30.Java代码   31.import java.io.File;     32.import java.io.FileInputStream;     33.import java.io.FileOutputStream;     34.import java.io.OutputStream;     35.    36.import org.apache.tools.zip.ZipEntry;     37.import org.apache.tools.zip.ZipOutputStream;     38.    39./**   40. * 文件夹压缩,支持win和linux   41. * @author wlzhang   42. *   43. */    44.public class ZipUtil     45.{     46.    /**   47.     * @param inputFileName   48.     *            输入一个文件夹   49.     * @param zipFileName   50.     *            输出一个压缩文件夹,打包后文件名字   51.     * @throws Exception   52.     */    53.    public static OutputStream zip(String inputFileName, String zipFileName) throws Exception     54.    {     55.        return zip(zipFileName, new File(inputFileName));     56.    }     57.    58.    private static OutputStream zip(String zipFileName, File inputFile) throws Exception     59.    {     60.        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(     61.                zipFileName));     62.        out.setEncoding("GBK");//解决linux乱码      63.        zip(out, inputFile, "");     64.        out.close();     65.        return out;     66.    }     67.    68.    private static void zip(ZipOutputStream out, File f, String base) throws Exception     69.    {     70.        if (f.isDirectory())     71.        { // 判断是否为目录      72.            File[] fl = f.listFiles();     73.            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));      74.//            out.putNextEntry(new ZipEntry(base + "/"));      75.            ZipEntry zipEntry=new ZipEntry(base + System.getProperties().getProperty("file.separator"));     76.            zipEntry.setUnixMode(755);//解决linux乱码      77.            out.putNextEntry(zipEntry);     78.//            base = base.length() == 0 ? "" : base + "/";      79.            base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator");     80.            for (int i = 0; i < fl.length; i++)     81.            {     82.                zip(out, fl[i], base + fl[i].getName());     83.            }     84.        }     85.        else    86.        { // 压缩目录中的所有文件      87.            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));      88.            ZipEntry zipEntry=new ZipEntry(base);     89.            zipEntry.setUnixMode(644);//解决linux乱码      90.            out.putNextEntry(zipEntry);     91.            FileInputStream in = new FileInputStream(f);     92.            int b;     93.            while ((b = in.read()) != -1)     94.            {     95.                out.write(b);     96.            }     97.            in.close();     98.        }     99.    }     100.}    101.  102.import java.io.File;  103.import java.io.FileInputStream;  104.import java.io.FileOutputStream;  105.import java.io.OutputStream;  106.  107.import org.apache.tools.zip.ZipEntry;  108.import org.apache.tools.zip.ZipOutputStream;  109.  110./** 111. * 文件夹压缩,支持win和linux 112. * @author wlzhang 113. * 114. */  115.public class ZipUtil  116.{  117.    /** 118.     * @param inputFileName 119.     *            输入一个文件夹 120.     * @param zipFileName 121.     *            输出一个压缩文件夹,打包后文件名字 122.     * @throws Exception 123.     */  124.    public static OutputStream zip(String inputFileName, String zipFileName) throws Exception  125.    {  126.        return zip(zipFileName, new File(inputFileName));  127.    }  128.  129.    private static OutputStream zip(String zipFileName, File inputFile) throws Exception  130.    {  131.        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(  132.                zipFileName));  133.        out.setEncoding("GBK");//解决linux乱码   134.        zip(out, inputFile, "");  135.        out.close();  136.        return out;  137.    }  138.  139.    private static void zip(ZipOutputStream out, File f, String base) throws Exception  140.    {  141.        if (f.isDirectory())  142.        { // 判断是否为目录   143.            File[] fl = f.listFiles();  144.            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));   145.//            out.putNextEntry(new ZipEntry(base + "/"));   146.            ZipEntry zipEntry=new ZipEntry(base + System.getProperties().getProperty("file.separator"));  147.            zipEntry.setUnixMode(755);//解决linux乱码   148.            out.putNextEntry(zipEntry);  149.//            base = base.length() == 0 ? "" : base + "/";   150.            base = base.length() == 0 ? "" : base + System.getProperties().getProperty("file.separator");  151.            for (int i = 0; i < fl.length; i++)  152.            {  153.                zip(out, fl[i], base + fl[i].getName());  154.            }  155.        }  156.        else  157.        { // 压缩目录中的所有文件   158.            // out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));   159.            ZipEntry zipEntry=new ZipEntry(base);  160.            zipEntry.setUnixMode(644);//解决linux乱码   161.            out.putNextEntry(zipEntry);  162.            FileInputStream in = new FileInputStream(f);  163.            int b;  164.            while ((b = in.read()) != -1)  165.            {  166.                out.write(b);  167.            }  168.            in.close();  169.        }  170.    }  171.}  172.  173.以上代码就同时支持win和linux   

?

热点排行