直接读取ZIP包数据 线上、线下方法

这段Java代码实现了从远程URL读取ZIP文件,并解析其中的TXT文件内容。通过HttpURLConnection连接到指定URL,使用ZipInputStream读取ZIP文件,遍历ZIP条目,找到TXT文件后按行读取内容,将每行数据按逗号分割并存储到PayPigeonLeadsDnModel对象中,最后输出对象信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static void main(String[] args) {

       //

       String path = "C:\\Users\\hengy\\Desktop\\2019042414.zip";

       String dnFileUrl =

           "http://mbncontent.oss-ap-southeast-1.aliyuncs.com/TH/C792871848C74AC4/DN/2019042414.zip";

       ZipInputStream zipInputStream = null;

       BufferedReader br = null;

       try {

           // new

           URL url = new URL(dnFileUrl);

           //

           HttpURLConnection conn = (HttpURLConnection)url.openConnection();

           Charset utf8 = Charset.forName("UTF-8");

           int resultCode = conn.getResponseCode();

           if (HttpStatus.OK.value() == resultCode) {

               zipInputStream = new ZipInputStream(conn.getInputStream(), utf8);

               ZipEntry zipEntry;

               while ((zipEntry = zipInputStream.getNextEntry()) != null) {

                   if (zipEntry.toString().endsWith("txt")) {

                       br = new BufferedReader(new InputStreamReader(zipInputStream));

                       String line;

                       while ((line = br.readLine()) != null) {

                           String[] infoArr = line.toString().split(",");

                           if (null != infoArr && infoArr.length > 1) {

                               PayPigeonLeadsDnModel dnModel = new PayPigeonLeadsDnModel();

                               dnModel.setTelco(infoArr[0]);

                               dnModel.setShortcode(infoArr[1]);

                               dnModel.setKeyword(infoArr[2]);

                               dnModel.setMtid(infoArr[3]);

                               dnModel.setMsisdn(infoArr[4]);

                               dnModel.setStatus(infoArr[5]);

                               if (infoArr.length >= 7) {

                                   dnModel.setStatusDetails(infoArr[6]);

                               }

                               //

                               System.out.println(dnModel.toString());

 

                           }

                       }

 

                   }

                   System.out.println();

               }

           }

 

       } catch (Exception e) {

           e.printStackTrace();

           LOGGER.error("Read zip file error. : {}", e.getMessage());

       } finally {

           try {

               if (null != br) {

                   br.close();

               }

               if (null != zipInputStream) {

                   zipInputStream.close();

               }

           } catch (IOException e) {

               e.printStackTrace();

               LOGGER.error("close zipInputStream error. : {}", e.getMessage());

           }

       }

 

       // //

       // ZipFile zipFile;

       // ZipInputStream zipInputStream = null;

       // try {

       // zipFile = new ZipFile(dnFileUrl);

       //

       // InputStream inputStream = new BufferedInputStream(new FileInputStream(dnFileUrl));

       // Charset utf8 = Charset.forName("UTF-8");

       // zipInputStream = new ZipInputStream(inputStream, utf8);

       // ZipEntry zipEntry;

       // while ((zipEntry = zipInputStream.getNextEntry()) != null) {

       // if (zipEntry.toString().endsWith("txt")) {

       // BufferedReader br =

       // new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));

       // String line;

       // while ((line = br.readLine()) != null) {

       // String[] infoArr = line.toString().split(",");

       // if (null != infoArr && infoArr.length > 1) {

       // PayPigeonLeadsDnModel dnModel = new PayPigeonLeadsDnModel();

       // dnModel.setTelco(infoArr[0]);

       // dnModel.setShortcode(infoArr[1]);

       // dnModel.setKeyword(infoArr[2]);

       // dnModel.setMtid(infoArr[3]);

       // dnModel.setMsisdn(infoArr[4]);

       // dnModel.setStatus(infoArr[5]);

       // if (infoArr.length >= 7) {

       // dnModel.setStatusDetails(infoArr[6]);

       // }

       // //

       // System.out.println(dnModel.toString());

       //

       // }

       // }

       // br.close();

       // }

       // System.out.println();

       // }

       // } catch (IOException e) {

       // e.printStackTrace();

       // LOGGER.error("Read zip file error. : {}", e.getMessage());

       // } finally {

       // try {

       // if (null != zipInputStream) {

       // zipInputStream.closeEntry();

       // }

       // } catch (IOException e) {

       // e.printStackTrace();

       // LOGGER.error("close zipInputStream error. : {}", e.getMessage());

       // }

       // }

   }

转载于:https://www.cnblogs.com/mask-xiexie/p/10880532.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值