java 压缩成gz文件

网站地图需要用到把.xml压缩成xml.gz文件格式

public class GZIPUtil {
    
    private static final Logger log = Logger.getLogger(GZIPUtil.class);
    
    public static void compressFile(String inFileName) {
        String outFileName = inFileName + ".gz";
        FileInputStream in = null;
        try {
            in = new FileInputStream(new File(inFileName));
        }catch (FileNotFoundException e) {
            log.debug("Could not find the inFile..."+inFileName);
            log.error(CommonUtil.stackToString(e));
        }
        
        GZIPOutputStream out = null;
        try {
            out = new GZIPOutputStream(new FileOutputStream(outFileName));
        }catch (IOException e) {
            log.debug("Could not find the outFile..."+outFileName);
            log.error(CommonUtil.stackToString(e));
        }
        byte[] buf = new byte[1024];
        int len = 0;
        try {
            while ((len = in.read()) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            log.debug("Completing the GZIP file..."+outFileName);
            out.flush();
            out.close();
        }catch (IOException e) {
            log.error(CommonUtil.stackToString(e));
        }
    }
    
    public static void main(String[] args) {
        String str = "D:/sitemap/Solution_sitemap.xml";
        compressFile(str);
    }
}

你可能感兴趣的:(J2ee,java,string,byte,null,class,file)