从压缩文件流中读取数据

public class ZipUtils {
	
	@Test
	public void getZipFile() throws IOException {
        String generateUrl = "http://文件地址";
		//从服务器请求文件流,具体代码就不贴了
//        CloseableHttpResponse response = HttpSender.toPost(FILE_URL, null);
//        InputStream inputStream = response.getEntity().getContent();
		URL url = new URL(generateUrl);
		URLConnection conn = url.openConnection();
		InputStream inputStream = conn.getInputStream();
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        byte[] buff = new byte[1024*1024]; //如果是稍微大的文件,这里配置的大一些
        int len = 0;
        while((len = inputStream.read(buff)) > 0) {
            //把从服务端读取的文件流保存到ByteArrayOutputSteam中
            byteArray.write(buff, 0, len);
            byteArray.flush();
        }
        inputStream.close();
//        response.close();

        //GZIPInputstream解压文件,然后读取文件
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(new GZIPInputStream(
                        new ByteArrayInputStream(byteArray.toByteArray())), "utf-8"));
        String line = null;
        while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }
	
}

参考:https://www.cnblogs.com/jianyong-long/p/9693061.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
您可以使用JavaZipInputStream类来读取zip压缩文件,然后将数据写入数据库。以下是一个简单的示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipFileReader { public static void main(String[] args) { String zipFilePath = "path/to/your/zip/file.zip"; String dbUrl = "jdbc:mysql://localhost:3306/mydatabase"; String dbUsername = "your-username"; String dbPassword = "your-password"; try { // 打开数据库连 Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword); // 创建插入数据的SQL语句 String insertQuery = "INSERT INTO your_table_name (file_name, file_data) VALUES (?, ?)"; // 创建PreparedStatement对象 PreparedStatement pstmt = conn.prepareStatement(insertQuery); // 创建ZipInputStream对象 ZipInputStream zipInput = new ZipInputStream(new FileInputStream(zipFilePath)); // 读取zip文件中的每个条目 ZipEntry entry = zipInput.getNextEntry(); while (entry != null) { // 读取条目的文件名和数据 String fileName = entry.getName(); byte[] fileData = new byte[(int) entry.getSize()]; zipInput.read(fileData); // 设置参数并执行插入语句 pstmt.setString(1, fileName); pstmt.setBytes(2, fileData); pstmt.executeUpdate(); // 关闭当前条目,准备读取下一个条目 zipInput.closeEntry(); entry = zipInput.getNextEntry(); } // 关闭ZipInputStream、PreparedStatement和数据库连 zipInput.close(); pstmt.close(); conn.close(); System.out.println("数据成功写入数据库!"); } catch (IOException | SQLException e) { e.printStackTrace(); } } } ``` 请将代码中的以下内容替换为您自己的信息: - `zipFilePath`:您的zip文件的路径。 - `dbUrl`:您的数据库URL。 - `dbUsername`:您的数据库用户名。 - `dbPassword`:您的数据库密码。 - `your_table_name`:您要插入数据的表名。 这段代码将逐个读取zip文件中的条目,并将每个条目的文件名和数据插入到数据库中。您需要根据自己的需求修改代码以适应您的数据库结构和数据处理逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值