多线程 Zip 文件读取器

发布于 2024-12-17 16:22:49 字数 240 浏览 0 评论 0原文

我正在进行的项目需要多线程 Zip 文件阅读器。当我说多线程时,我的意思是例如有两个线程并且都尝试读取同一个文件,所以我需要在两个单独的线程上创建输入流的东西,以便 zip 文件打开不会被损坏。我想知道是否有任何可用的现有代码可以完成这项工作,因为我不想重新发明轮子。

另外,如果代码只为我提供在单独线程上创建的缓冲读取器的新实例,这样我就可以使用读取器并继续我的工作,这也是很好的。此外,代码还必须处理线程池,并在工作完成后将其自身从线程池中删除。

I had a requirement for Multithreaded Zip file reader for my ongoing project.When i say multithreaded I mean for example there are two threads and both try to read the same file so I need something which creates Inputstreams on two seperate threads so the zip file dosen't become corrupted. I would like to know that if there is any existing code available which can do the job as I don't want to reinvent the wheel.

Also it would be good that the code just give me new instances of buffered reader created on separated threads so that I can just used the reader and carry on with my work. In addition the code must also take care of the thread pool and remove himself from thread pool when work done.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-12-24 16:22:49

首先,正如我在上面的评论中所写,磁盘 IO 是一个瓶颈,有时当已经有一个打开的文件描述符时您无法访问文件。

这是一个很大的问题,但作为一些深思熟虑,您可以在每个线程中打开一个 FileInputStream ,然后在它们之间同步您正在读取的位置。

假设我们有两个流:

FileInputStream f1 = new FileInputStream("test.zip"),
  f2 = new FileInputStream("test.zip");

您可以像平常一样读取,然后在一个线程中读取一些字节后skip(long n)。假设您使用 f1 读取 10 个字节,那么您需要在读取 f2 之前执行 f2.skip(10),因为它会返回否则相同的字节。当然,假设这就是您想要的。

读取 Zip 文件的标头后,您可以确定存档的所有各个部分在文件中的位置,并分别读取这些确切的块。但是,正如 Dave Newton 正确指出的那样,您应该使用定义良好的库来读取 Zip 文件。

First off, as I wrote in my comment up top, disk IO is a bottleneck and sometimes you can't access a file when theres's already one open file descriptor.

It is quite a question to impose, but as some food for thought you could open a FileInputStream in each thread and then synchronize between them the position you're reading from.

Assuming we have two streams:

FileInputStream f1 = new FileInputStream("test.zip"),
  f2 = new FileInputStream("test.zip");

You can read as you usually do and then skip(long n) after reading some bytes in one thread. Say you read 10 bytes using f1 then you'd want to execute f2.skip(10) before reading in f2 because it would return the same bytes otherwise. Assuming this is what you want, of course.

After reading the header of the Zip file you can determine where all the individual parts of the archive are located within the file and read those exact blocks separately. But, as Dave Newton correctly states, you should use well-defined libraries for reading the Zip files, though.

与往事干杯 2024-12-24 16:22:49

我建议首先阅读 Java 中的多线程 - 这是了解问题的最佳方式,这将引导您了解解决方案的更好路径(如果您坚持尝试多线程 IO)。快速的 Google 搜索给我带来了 这些不过,我确实同意 netrom 所说的。

I would suggest reading up on multhreading in Java first - it's the best way to learn about the problem, which will lead you to understand a better path to the solution (if you insist on attempting multithreaded IO. A quick Google search brought me these results. However, I do agree on what netrom said.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文