Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.3 kB
2
Indexable
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"))) {
        if (entry.getName().equalsIgnoreCase("type.txt")) {
            // If the file is type.txt, replace double quotes with two single quotes
            StringBuilder modifiedContent = new StringBuilder();
            int c;
            while ((c = reader.read()) != -1) {
                if (c == '"') {
                    modifiedContent.append("''");
                } else {
                    modifiedContent.append((char) c);
                }
            }
            long rowsInserted = copyManager.copyIn(sql, new StringReader(modifiedContent.toString()));
            System.out.println("Inserted " + rowsInserted + " rows into table " + tableName + " (with quote replacement)");
        } else {
            long rowsInserted = copyManager.copyIn(sql, reader);
            System.out.println("Inserted " + rowsInserted + " rows into table " + tableName);
        }
    }
}
Leave a Comment