zoukankan      html  css  js  c++  java
  • java不解压tar.gz读取包里面的某个文件内容或读取远程zip包中的文件内容

    <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));
                }
            }
        }
    }
    

      

  • 相关阅读:
    You need to use a Theme.AppCompat theme
    Objective-C中nil与release的区别与用法
    objective-c中自己创建的对象为什么不能调用release
    NSString类的相关用法
    [Bug-IOS]
    中国有什么旅游社交网站吗?
    om.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException
    【剑指Offer学习】【面试题22:栈的压入、弹出序列】
    2-08. 用扑克牌计算24点(25) (ZJU_PAT 数学 枚举)
    在北京工作了两年,如今跳槽到了广州,社保公积金该怎样办理?
  • 原文地址:https://www.cnblogs.com/james-roger/p/12880198.html
Copyright © 2011-2022 走看看