<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.18</version> </dependency>
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(); } }
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包中的文件内容
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)); } } } }