9cc2efd5242ba4b2d7d7ab6230f634288051cc3b.svn-base 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package xin.glue.ui.common;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.io.OutputStreamWriter;
  11. import java.io.PrintWriter;
  12. import java.io.UnsupportedEncodingException;
  13. import java.util.zip.GZIPOutputStream;
  14. import javax.xml.parsers.DocumentBuilder;
  15. import javax.xml.parsers.DocumentBuilderFactory;
  16. import javax.xml.parsers.ParserConfigurationException;
  17. import javax.xml.transform.OutputKeys;
  18. import javax.xml.transform.Transformer;
  19. import javax.xml.transform.TransformerConfigurationException;
  20. import javax.xml.transform.TransformerException;
  21. import javax.xml.transform.TransformerFactory;
  22. import javax.xml.transform.dom.DOMSource;
  23. import javax.xml.transform.stream.StreamResult;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.Element;
  26. import com.alibaba.fastjson.JSONArray;
  27. import com.alibaba.fastjson.JSONObject;
  28. public class XmlOutput {
  29. private static Document document;
  30. public static final int BUFFER = 1024;
  31. public static final String EXT = ".gz";
  32. public static void init() {
  33. try {
  34. DocumentBuilderFactory factory = DocumentBuilderFactory
  35. .newInstance();
  36. DocumentBuilder builder = factory.newDocumentBuilder();
  37. document = builder.newDocument();
  38. } catch (ParserConfigurationException e) {
  39. System.out.println(e.getMessage());
  40. }
  41. }
  42. public static void createXml(String fileName, String jsonStr,
  43. String[] colRef, String[] title) {
  44. init();
  45. Element root = document.createElement("rows"); // 创建根节点
  46. document.appendChild(root);
  47. /* 创建子节点,start */
  48. Element item;
  49. Element ele;
  50. String[] colRefs = colRef[0].split("\\^");
  51. String[] ts = title[0].split("\\|");
  52. String[] titles = ts[ts.length-1].split("\\^");
  53. JSONArray array = JSONArray.parseArray(jsonStr);
  54. for (int i = 0; i < array.size(); i++) {
  55. item = document.createElement("row");
  56. item.setAttribute("index", String.valueOf(i + 1));
  57. JSONObject jobj = (JSONObject) array.get(i);
  58. for (int j = 0; j < colRefs.length; j++) {
  59. ele = document.createElement(colRefs[j]);
  60. ele.setAttribute("desc", titles[j]);
  61. ele.setTextContent(jobj.getString(colRefs[j]));
  62. item.appendChild(ele);
  63. }
  64. root.appendChild(item);
  65. }
  66. /* end */
  67. // 将DOM对象document写入到xml文件中
  68. TransformerFactory tf = TransformerFactory.newInstance();
  69. try {
  70. Transformer transformer = tf.newTransformer();
  71. DOMSource source = new DOMSource(document);
  72. transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
  73. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  74. PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName),"utf-8"));
  75. StreamResult result = new StreamResult(pw);
  76. transformer.transform(source, result); // 关键转换
  77. pw.close();
  78. // System.out.println("生成XML文件成功!");
  79. } catch (TransformerConfigurationException e) {
  80. System.out.println(e.getMessage());
  81. } catch (IllegalArgumentException e) {
  82. System.out.println(e.getMessage());
  83. } catch (FileNotFoundException e) {
  84. System.out.println(e.getMessage());
  85. } catch (TransformerException e) {
  86. System.out.println(e.getMessage());
  87. } catch (UnsupportedEncodingException e) {
  88. System.out.println(e.getMessage());
  89. }
  90. }
  91. /**
  92. * 数据压缩
  93. *
  94. * @param data
  95. * @return
  96. * @throws Exception
  97. */
  98. public static byte[] compress(byte[] data) throws Exception {
  99. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  100. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  101. // 压缩
  102. compress(bais, baos);
  103. byte[] output = baos.toByteArray();
  104. baos.flush();
  105. baos.close();
  106. bais.close();
  107. return output;
  108. }
  109. /**
  110. * 数据压缩
  111. *
  112. * @param is
  113. * @param os
  114. * @throws Exception
  115. */
  116. public static void compress(InputStream is, OutputStream os)
  117. throws Exception {
  118. GZIPOutputStream gos = new GZIPOutputStream(os);
  119. int count;
  120. byte data[] = new byte[BUFFER];
  121. while ((count = is.read(data, 0, BUFFER)) != -1) {
  122. gos.write(data, 0, count);
  123. }
  124. gos.finish();
  125. gos.flush();
  126. gos.close();
  127. }
  128. /**
  129. * 文件压缩
  130. *
  131. * @param path
  132. * @throws Exception
  133. */
  134. public static void compress(String path) throws Exception {
  135. compress(path, true);
  136. }
  137. /**
  138. * 文件压缩
  139. *
  140. * @param path
  141. * @param delete
  142. * 是否删除原始文件
  143. * @throws Exception
  144. */
  145. public static void compress(String path, boolean delete) throws Exception {
  146. File file = new File(path);
  147. compress(file, delete);
  148. }
  149. /**
  150. * 文件压缩
  151. *
  152. * @param file
  153. * @param delete
  154. * 是否删除原始文件
  155. * @throws Exception
  156. */
  157. public static void compress(File file, boolean delete) throws Exception {
  158. FileInputStream fis = new FileInputStream(file);
  159. FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);
  160. compress(fis, fos);
  161. fis.close();
  162. fos.flush();
  163. fos.close();
  164. if (delete) {
  165. file.delete();
  166. }
  167. }
  168. }