Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
1.7 kB
3
Indexable
Never
private void processZipFile(File zipFile, Connection dbConnection) throws SQLException, IOException {
        System.out.println("Processing ZIP file: " + zipFile.getName());
        try (FileInputStream fis = new FileInputStream(zipFile);
             ZipArchiveInputStream zis = new ZipArchiveInputStream(fis)) {

            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (!entry.isDirectory() && ACCEPTED_FILES.contains(entry.getName())) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = zis.read(buffer)) != -1) {
                        baos.write(buffer, 0, len);
                    }
                    processZipEntry(new ByteArrayInputStream(baos.toByteArray()), entry, dbConnection);
                }
            }
        }
    }

    private void processZipEntry(InputStream zis, ZipEntry entry, Connection dbConnection) throws SQLException, IOException {
        String tableName = entry.getName().split("\\.")[0];
        System.out.println("Processing table: " + tableName);

        CopyManager copyManager = new CopyManager((BaseConnection) dbConnection);
        String sql = String.format("COPY %s FROM STDIN WITH CSV HEADER DELIMITER E'\t'", tableName);

        try (Reader reader = new BufferedReader(new InputStreamReader(zis, "Cp1250"))) {
            long rowsInserted = copyManager.copyIn(sql, reader);
            System.out.println("Inserted " + rowsInserted + " rows into table " + tableName);
        }
    }
Leave a Comment