/j2se/LGame-j2se-0.2.6/org/loon/framework/game/simple/utils/CompressionUtils.java

http://loon-simple.googlecode.com/ · Java · 194 lines · 132 code · 11 blank · 51 comment · 18 complexity · 18a481ac5a771e03c47372a3ee3a6497 MD5 · raw file

  1. package org.loon.framework.game.simple.utils;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileNotFoundException;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.util.zip.GZIPInputStream;
  12. import java.util.zip.GZIPOutputStream;
  13. import java.util.zip.ZipEntry;
  14. import java.util.zip.ZipInputStream;
  15. /**
  16. * Copyright 2008 - 2009
  17. *
  18. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  19. * use this file except in compliance with the License. You may obtain a copy of
  20. * the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  26. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  27. * License for the specific language governing permissions and limitations under
  28. * the License.
  29. *
  30. * @project loonframework
  31. * @author chenpeng
  32. * @email?ceponline@yahoo.com.cn
  33. * @version 0.1
  34. */
  35. public abstract class CompressionUtils {
  36. /**
  37. * ???Zip?????????????byte[]
  38. *
  39. * @param fileName
  40. * @param fname
  41. * @return
  42. */
  43. public static byte[] readBytesFormZipFile(String fileName, String fname) {
  44. byte[] bytes = null;
  45. ByteArrayOutputStream os = null;
  46. ZipInputStream in = null;
  47. try {
  48. os = new ByteArrayOutputStream(4096);
  49. in = new ZipInputStream(new FileInputStream(fileName));
  50. boolean found = false;
  51. while (!found) {
  52. ZipEntry entry = in.getNextEntry();
  53. if (fname.equalsIgnoreCase(entry.getName())) {
  54. found = true;
  55. }
  56. }
  57. int read;
  58. byte[] buffer = new byte[4096];
  59. while ((read = in.read(buffer)) >= 0) {
  60. os.write(buffer, 0, read);
  61. }
  62. bytes = os.toByteArray();
  63. } catch (Exception e) {
  64. bytes = null;
  65. } finally {
  66. try {
  67. if (os != null) {
  68. os.flush();
  69. os = null;
  70. }
  71. if (in != null) {
  72. in.close();
  73. in = null;
  74. }
  75. } catch (Exception e) {
  76. }
  77. }
  78. return bytes;
  79. }
  80. /**
  81. * ????byte[]??
  82. *
  83. * @param bytes
  84. * @return
  85. */
  86. public static final byte[] compress(byte[] bytes) {
  87. if (bytes == null) {
  88. throw new NullPointerException("byte[] is NULL !");
  89. }
  90. ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
  91. try {
  92. GZIPOutputStream gzip = new GZIPOutputStream(bos);
  93. gzip.write(bytes, 0, bytes.length);
  94. gzip.finish();
  95. byte[] fewerBytes = bos.toByteArray();
  96. gzip.close();
  97. bos.close();
  98. gzip = null;
  99. bos = null;
  100. return fewerBytes;
  101. } catch (IOException e) {
  102. return null;
  103. }
  104. }
  105. /**
  106. * ?????byte[]
  107. *
  108. * @param bytes
  109. * @return
  110. */
  111. public static final byte[] uncompress(final byte[] bytes) {
  112. if (bytes == null) {
  113. throw new NullPointerException("byte[] is NULL !");
  114. }
  115. ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  116. ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
  117. byte[] buffer = new byte[bytes.length];
  118. int length;
  119. try {
  120. GZIPInputStream gis = new GZIPInputStream(bais);
  121. while ((length = gis.read(buffer)) != -1) {
  122. bos.write(buffer, 0, length);
  123. }
  124. byte[] moreBytes = bos.toByteArray();
  125. bos.close();
  126. bais.close();
  127. gis.close();
  128. bos = null;
  129. bais = null;
  130. gis = null;
  131. return moreBytes;
  132. } catch (IOException e) {
  133. return null;
  134. }
  135. }
  136. /**
  137. * ?????zip??
  138. *
  139. * @param zipFile
  140. * @throws FileNotFoundException
  141. * @throws IOException
  142. */
  143. public void decompressFile(File zipFile) throws FileNotFoundException,
  144. IOException
  145. {
  146. BufferedOutputStream dest = null;
  147. FileInputStream fis = new FileInputStream(zipFile);
  148. ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
  149. String basePath = zipFile.getAbsolutePath().substring(0,
  150. zipFile.getAbsolutePath().lastIndexOf(File.separatorChar));
  151. ZipEntry entry;
  152. while ((entry = zis.getNextEntry()) != null) {
  153. if (entry.isDirectory()) {
  154. File folderFile = new File(basePath + "\\" + entry.getName());
  155. folderFile.mkdirs();
  156. continue;
  157. }
  158. int count;
  159. byte data[] = new byte[8192];
  160. FileOutputStream fos = new FileOutputStream(basePath
  161. + File.separatorChar + entry.getName());
  162. dest = new BufferedOutputStream(fos, 8192);
  163. while ((count = zis.read(data, 0, 8192)) != -1) {
  164. dest.write(data, 0, count);
  165. }
  166. dest.flush();
  167. dest.close();
  168. }
  169. zis.close();
  170. }
  171. /**
  172. * ???????
  173. *
  174. * @param fileName
  175. */
  176. public void decompressFile(String fileName) {
  177. try {
  178. decompressFile(new File(fileName));
  179. } catch (Exception e) {
  180. throw new RuntimeException(e);
  181. }
  182. }
  183. }