乐趣区

关于java:原始jdbc连接数据读取字符串生成zip文件并读成byte数组

代码如下:
本次数据库为 sqlserve

 try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=cs", "sa", "Zmy123456.");
          String sql = "select ATFile,ATFileName from AT_AttachmentFile where ATGuid ='A1:05c5bf44-2323-48ec-8f23-0f6623ee7fcd'";

          PreparedStatement ps =connection.prepareStatement(sql);
          ResultSet rs = ps.executeQuery();
          ResultSetMetaData rsmd = rs.getMetaData();
          Map<String, Object> mapMetaData = new HashMap<>();
          ZipFile zipFile = null;
          Stream<? extends ZipEntry> signStream = null;
          Stream<? extends ZipEntry> zipStream = null;
          while (rs.next()) {String columnLabel = rsmd.getColumnLabel(1);
              Blob columnValue = rs.getBlob(columnLabel);
              InputStream ins= columnValue.getBinaryStream();
              File file = new File("D:\\","cs.zip");
              org.apache.commons.io.FileUtils.copyInputStreamToFile(ins, file);
              zipFile = new ZipFile(file, Charset.defaultCharset());
              signStream = zipFile.stream();
              zipStream = zipFile.stream();
              // 断言
              Predicate<ZipEntry> signTxt = ze -> ze.getName().contains("sign,txt");
              Predicate<ZipEntry> zipTxt = ze -> ze.getName().endsWith(".zip");
              //,过滤
              Optional<ZipEntry> signInfo = (Optional<ZipEntry>) signStream.filter(signTxt).findFirst();
              Optional<ZipEntry> zipInfo = (Optional<ZipEntry>) zipStream.filter(zipTxt).findFirst();
              ins.close();
              zipFile.close();
              // 获取文件名称
              String fileName = rsmd.getColumnLabel(2);
              Object fileNames = rs.getObject(fileName);
              String zipDir = "D:\\";
              // 解压 zip 文件
              String name = ZipUtils.unzip(file.getPath(),zipDir);
              file.delete();
              // 读成 byte 数组返回
              byte[]  bytes = FileUtil.readFile2Bytes(new File(zipDir+File.separator+name),true);
              
              System.out.println(bytes);

          }



          // 将字符串写入到 zip 中如下
//            File file = new File("D:\\","cs.zip");
//            java.util.zip.ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
//            mapMetaData.forEach((k,v)->{
//                try {//                    zipOutputStream.putNextEntry(new ZipEntry("15.pdf"));
//                    IoUtil.write(zipOutputStream,false, (byte[]) v);
//                    zipOutputStream.flush();
//                    zipOutputStream.closeEntry();
//                } catch (IOException e) {//                    e.printStackTrace();
//                }
//            });
      } catch (Exception e) {e.printStackTrace();
      }

解压文件办法 unzip

// 解压 zip 文件
    public static String unzip(String filePath,String zipDir){
        String name = null;
        BufferedOutputStream des = null;
        BufferedInputStream is =null;
        ZipFile zipFile = null;
        try {
            ZipEntry entry;
            zipFile = new ZipFile(filePath);
            Enumeration dir = zipFile.entries();
            while (dir.hasMoreElements()){entry = (ZipEntry) dir.nextElement();
                if (entry != null){name = entry.getName();
                }
            }
            Enumeration e = zipFile.entries();
            while (e.hasMoreElements()){entry = (ZipEntry) e.nextElement();
                if (entry.isDirectory()){continue;}else {is = new BufferedInputStream(zipFile.getInputStream(entry));
                    int count;
                    byte[] bytes = new byte[2048];
                    FileOutputStream fos = new FileOutputStream(zipDir+entry.getName());
                    des = new BufferedOutputStream(fos,2048);
                    while ((count = is.read(bytes,0,2048)) !=-1){des.write(bytes,0,count);
                    }

                }
            }

            return name;
        } catch (IOException e) {e.printStackTrace();
        }finally {
            try {zipFile.close();
                des.flush();
                des.close();
                is.close();} catch (IOException e) {e.printStackTrace();
            }

        }
       return name;
    };

读成 byte 数组

 public static byte[] readFile2Bytes(File file, boolean isDelete) throws IOException {InputStream inputStream = new FileInputStream(file);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            int len = 0;
            byte[] b = new byte[10240];
            while ((len = inputStream.read(b)) != -1) {outputStream.write(b, 0, len);
            }
            byte[] byteArray = outputStream.toByteArray();
            return byteArray;
        } finally {IOUtils.close(inputStream);
            IOUtils.close(outputStream);
            if (isDelete) {file.delete();
            }
        }
    }
退出移动版