1 2 3 4 5 | <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version> 1.18 </version> </dependency> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.example.demo.xml2map; import java.io.*; /** * @Author: luojie * @Date: 2020/5/13 8:19 */ public class FileUtil { public static byte [] getContent(File file) { try { return getContent( new FileInputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } return new byte []{}; } public static byte [] getContent(InputStream is) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { byte [] buffer = new byte [ 1024 ]; //byte[] buffer = new byte[16 * 1024]; while ( true ) { int len = is.read(buffer); if (len == - 1 ) { break ; } baos.write(buffer, 0 , len); } } catch (Exception e) { e.printStackTrace(); } return baos.toByteArray(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.example.demo.xml2map; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.lang3.StringUtils; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.util.zip.GZIPInputStream; /** * @Author: luojie * @Date: 2020/5/13 8:21 */ public class TarGz2Test { public static void readTarFile(File tarFile){ String orginFileName = tarFile.getName(); try { ArchiveInputStream archiveInputStream = null ; if (StringUtils.endsWithIgnoreCase(tarFile.getName(), ".gz" )) { archiveInputStream = new ArchiveStreamFactory() .createArchiveInputStream( "tar" , new GZIPInputStream( new BufferedInputStream( new FileInputStream(tarFile)))); } else { archiveInputStream = new ArchiveStreamFactory() .createArchiveInputStream( "tar" , new BufferedInputStream( new FileInputStream(tarFile))); } TarArchiveEntry entry = null ; while ((entry = (TarArchiveEntry) archiveInputStream.getNextEntry()) != null ) { if (entry.getSize() > 0 ) { String fileName = orginFileName.substring( 0 , orginFileName.indexOf( ".tar.gz" )) + "-MSS1.xml" ; if (fileName.equalsIgnoreCase(entry.getName())){ System.out.println( "fileSize is " + entry.getSize()); byte [] bytes = FileUtil.getContent(archiveInputStream); System.out.println( new String(bytes)); } } } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { readTarFile( new File( "E:\\satellite\\GF1_PMS1_E109.2_N38.3_20170803_L1A0002525088.tar.gz" )); // System.out.println(JSON.toJSONString(data)); } } |
读取远程zip包中的文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.example.demo.xml2map; import com.alibaba.fastjson.JSON; import java.io.BufferedInputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.Charset; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import static com.example.demo.xml2map.TarGz2Test.xmlToMap; /** * @Author: luojie * @Date: 2020/6/12 9:24 */ public class RemoteXmlTest { public static void main(String[] args) throws Exception{ URL urlfile = new URL( "http://192.168.190.153:80/group1/M00/00/00/wKi-mV7i2yGAMAf_AAABDdSbEN0882.zip" ); HttpURLConnection conn = (HttpURLConnection) urlfile.openConnection(); BufferedInputStream bis = null ; Charset gbk = Charset.forName( "utf-8" ); bis = new BufferedInputStream(conn.getInputStream()); ZipInputStream zin = new ZipInputStream(bis, gbk); ZipEntry ze; while ((ze = zin.getNextEntry()) != null ) { if (ze.getName().equalsIgnoreCase( "user.xml" )){ byte [] bytes = FileUtil.getContent(zin); Map<String, String> map = xmlToMap(bytes); System.out.println(JSON.toJSONString(map)); } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了