Gzip压缩文件和压缩字符串,web接口应用
Gzip压缩文件和压缩字符串,web接口应用
1.压缩文件
package com.example.core.mydemo.gzip; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * 压缩文件 */ public class GzipUtil { public static void compressFile(String sourceFile, String compressedFile) { try { FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(compressedFile); GZIPOutputStream gzos = new GZIPOutputStream(fos); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { gzos.write(buffer, 0, len); } gzos.finish(); gzos.close(); fos.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } public static void decompressFile(String compressedFile, String decompressedFile) { try { FileInputStream fis = new FileInputStream(compressedFile); FileOutputStream fos = new FileOutputStream(decompressedFile); GZIPInputStream gzis = new GZIPInputStream(fis); byte[] buffer = new byte[1024]; int len; while ((len = gzis.read(buffer)) != -1) { fos.write(buffer, 0, len); } gzis.close(); fos.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } package com.example.core.mydemo.gzip; public class GzipUtilExample { public static void main(String[] args) { // 压缩文件 String sourceFile = "F:\\scooterOrderCommon241021.log"; String compressedFile = "F:\\compressed2.gz"; GzipUtil.compressFile(sourceFile, compressedFile); // 解压缩文件 String decompressedFile = "F:\\decompressed2.txt"; GzipUtil.decompressFile(compressedFile, decompressedFile); } }
2.压缩字符串
package com.example.core.mydemo.gzip; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * gzip字符串压缩的使用 * 使用gzip对字符串进行压缩可以帮助我们在网络传输、文件压缩等场景中减小数据体积,提高传输效率。在实际应用中,我们可以将压缩后的字节数组进行传输或保存到文件中,然后在需要时解压缩并恢复原始字符串。 * * output: Compressed string size before: 200 Compressed string size after: 72 byte转字符串(字节大小): 146 byte转字符串= �H����Q(��,V �D�����⒢̼t���"�������܂������<=�A� ����� 解压缩字符串=java.io.ByteArrayInputStream@65b3120a compressed addr=[B@6f539caf compressed2 addr=[B@79fc0f2f 解压缩字符串2=Hello, this is a test string for gzip compression.Hello, this is a test string for gzip compression.Hello, this is a test string for gzip compression.Hello, this is a test string for gzip compression. res=abcd */ public class GzipHelper { /** * 压缩方法 * @param input * @return * @throws IOException */ public static byte[] compressString(String input) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos); gzip.write(input.getBytes()); gzip.flush(); gzip.close(); return bos.toByteArray(); } private static StringBuffer unCompressed(byte[] compressed2) throws IOException { /** * 解压缩 - 正解 */ InputStream in = new ByteArrayInputStream(compressed2); GZIPInputStream gzin = new GZIPInputStream(in); BufferedReader reader = new BufferedReader(new InputStreamReader(gzin,"UTF-8")); String line; StringBuffer sb = new StringBuffer(); while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); return sb; } public static void main(String[] args) throws IOException { String input = "Hello, this is a test string for gzip compression.Hello, this is a test string for gzip compression.Hello, this is a test string for gzip compression.Hello, this is a test string for gzip compression."; System.out.println("Compressed string size before: " + input.getBytes().length); byte[] compressed = compressString(input); System.out.println("Compressed string size after: " + compressed.length); //byte转字符串 >> 输出错误 不能通过这种方式转字符串,需要解压缩的方式来处理。 String ss = new String(compressed, StandardCharsets.UTF_8); System.out.println("byte转字符串(字节大小): " + ss.getBytes().length); System.out.println("byte转字符串=" + ss); //输出错误 // byte[] compressed2 = compressed; //会报错:Exception in thread "main" java.util.zip.ZipException: Not in GZIP format //需要克隆对象,使用同一个对象会报错。 解决以上报错 byte[] compressed2 = compressed.clone(); //压缩接口传输 //gzip压缩的逆运算 ByteArrayInputStream ios = new ByteArrayInputStream(compressed); GZIPInputStream gzip = new GZIPInputStream(ios); gzip.read(compressed); gzip.close(); String ss2 = ios.toString(); ios.close(); System.out.println("解压缩字符串=" + ss2); //Exception in thread "main" java.util.zip.ZipException: Not in GZIP format /** * 报错:"Not in GZIP format" 通常出现在处理压缩文件时,尤其是在解压GZIP格式的文件时。这个错误表明你尝试解压的文件并不是有效的GZIP格式,可能是因为文件已损坏、不完整或者根本不是GZIP压缩过的。 * 打印出来是2个地址 */ System.out.println("compressed addr=" + compressed.toString()); System.out.println("compressed2 addr=" + compressed2.toString()); StringBuffer sb = unCompressed(compressed2); System.out.println("解压缩字符串2=" + sb); //普通字符串与byte转换,以上是gzip压缩的字符串与byte转换。 String str = "abcd"; byte[] bs = str.getBytes(); String res = new String(bs,"UTF-8"); System.out.println("res=" + res); } }
3.web应用demo
import com.test.commons.utils.GsonUtils; import com.test.commons.web.ErrorCode; import com.test.insurancedock.service.EhCacheService; import com.test.insurancedock.vo.req.UserVo; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @RestController @RequestMapping("/gzip") @lombok.extern.slf4j.Slf4j public class GzipController { @Autowired private EhCacheService ehCacheService; private static final String ENCODING_UTF8 = "UTF-8"; protected static ObjectMapper mapper = new ObjectMapper(); /** * 方法返回类型:void * @param request * @param response */ @PostMapping("/saveUser") public void saveUser(HttpServletRequest request, HttpServletResponse response) { BufferedReader reader = null; try { long start = System.currentTimeMillis(); log.info("添加开始:" + start); GZIPInputStream gzipIn = new GZIPInputStream(request.getInputStream()); reader = new BufferedReader(new InputStreamReader(gzipIn,"UTF-8")); String reqContent = reader.readLine(); UserVo userVo = null; if(StringUtils.hasText(reqContent)){ userVo = mapper.readValue(reqContent,UserVo.class); } log.info("接收到的参数值:" + GsonUtils.toJson(userVo)); String res = ehCacheService.updateUser(userVo.getId(),userVo.getName()); System.out.println("添加成功的记录=" + res); long end = System.currentTimeMillis(); log.info("添加结束:" + end); log.info("添加耗时:" + (end - start)); writeResponse(response, "json串返回成功"); }catch (Exception e) { e.printStackTrace(); }finally{ if(reader != null){ try { reader.close(); } catch (Exception e) { log.error("",e); } } } writeResponse(response, "json串返回失败"); } protected void writeResponse(HttpServletResponse response, Object outJB){ // response.setContentType("text/html; charset=utf-8"); response.setContentType("application/json; charset=utf-8"); //response.setContentType("text/json; charset=utf-8"); response.setCharacterEncoding(ENCODING_UTF8); response.setHeader("Content-Encoding", "gzip"); GZIPOutputStream out = null; //byte[] jsonOut = null; log.info("response start."); String jsonOut = null; try { jsonOut = mapper.writeValueAsString(outJB); out = new GZIPOutputStream(response.getOutputStream()); out.write(jsonOut.getBytes(ENCODING_UTF8)); out.flush(); } catch (Exception e) { log.error("output error",e); } finally{ if(out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } } //单元测试类: package com.test.insurancedock; import org.junit.Test; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class GzipTest { private static final String reqUrl = "http://localhost:1340/"; @Test public void testGzip() { String url = "gzip/saveUser"; // 获取TN码 String strReq = "{\"id\":\"10\",\"name\":\"zhangliao\"}"; try { String result = appPost(strReq, reqUrl + url); System.err.println("接口调用返回结果:" + result); } catch (Exception e) { e.printStackTrace(); } } /* * @param str json数据格式 * @param reqUrl */ public static String appPost(String str, String reqUrl) throws Exception{ //发送数据 HttpURLConnection conn = null; StringBuffer sb = new StringBuffer(); try { URL url = new URL(reqUrl); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setInstanceFollowRedirects(false);//是否自动处理重定向 conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "no"); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); conn.connect(); GZIPOutputStream out = new GZIPOutputStream(conn.getOutputStream()); byte[] reqContent = str.getBytes(); System.out.println("testPost发送内容:"+new String(reqContent,"UTF-8")); out.write(reqContent); out.flush(); out.close(); //接收返回数据 InputStream in = conn.getInputStream(); GZIPInputStream gzin = new GZIPInputStream(in); BufferedReader reader = new BufferedReader(new InputStreamReader(gzin,"UTF-8")); String line; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); return sb.toString(); } catch (Exception e) { e.printStackTrace(); } finally{ if(conn != null){ conn.disconnect(); } } return sb.toString(); } }
打印输出:
testPost发送内容:{"id":"10","name":"zhangliao"}
接口调用返回结果:"json串返回成功"