标签:comm 文件 zip trace == stream package buffere lang
<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));
}
}
标签:comm 文件 zip trace == stream package buffere lang
原文地址:https://www.cnblogs.com/james-roger/p/12880198.html