gpt4 book ai didi

java - 是否有 GetCompressedFileSize 的 Java 等价物?

转载 作者:行者123 更新时间:2023-11-30 09:27:06 24 4
gpt4 key购买 nike

我希望获得 Java 中稀疏文件的准确测量值(即磁盘上的实际大小,而不是包括所有 0 的正常大小)。

在 Windows 上的 C++ 中,可以使用 GetCompressedFileSize .我还没有遇到如何在 Java 中做到这一点?

如果没有直接的等价物,我将如何测量稀疏文件中的数据,而不是包括所有零的大小?

为了澄清,我正在寻找它来在 Linux 操作系统和 Windows 上运行稀疏文件测量,但是我不介意编写两个单独的应用程序!

最佳答案

如果您想要纯 Java 解决方案,可以尝试 jnr-posix .这是一个 example implementation

import jnr.posix.*;

final POSIX p = POSIXFactory.getPOSIX();
final int S_BLKSIZE = 512; // from sys/stat.h
final FileStat stat = p.stat("/path/to/file");
final long bytes = stat.blocks() * S_BLKSIZE;

但是目前函数won't work for Windows .在修复之前,您必须使用如下所示的特定于平台的代码

  • 在 Linux 上使用 stat64系统调用

    The st_blocks field indicates the number of blocks allocated to the file, 512-byte units. (This may be smaller than st_size/512 when the file has holes.)

    • 您还可以运行 stat命令。分配的 block 数可以在Blocks字段中看到,或者用%b格式说明符打印
    • 或使用du command (没有 --apparent-size 选项)

      --apparent-size

      • print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like
  • 在 Windows 上,您可以调用 GetCompressedFileSize接口(interface)

    • 或者,您也可以使用管理员权限运行 fsutil file layout 以获取有关文件的详细信息。找到 $DATA 流。

      • 如果您看到 Resident |在这样的标志中没有分配簇,那么它是一个常驻文件,磁盘上的大小将为 0。

        PS C:\Users>  fsutil file layout .\desktop.ini

        ********* File 0x000800000003dbde *********
        File reference number : 0x000800000003dbde
        File attributes : 0x00000026: Hidden | System | Archive
        File entry flags : 0x00000000
        Link (ParentID: Name) : 0x001f0000000238c8: HLINK Name : \Users\desktop.ini
        ...
        Stream : 0x080 ::$DATA
        Attributes : 0x00000000: *NONE*
        Flags : 0x0000000c: Resident | No clusters allocated
        Size : 174
        Allocated Size : 176
      • 如果您没有看到驻留标志,请检查分配大小 字段,它是文件在磁盘上的大小

        PS D:\>  fsutil file layout .\nonresident.txt

        ********* File 0x000400000000084e *********
        File reference number : 0x000400000000084e
        File attributes : 0x00000020: Archive
        File entry flags : 0x00000000
        Link (ParentID: Name) : 0x0005000000000005: HLINK Name : \nonresident.txt
        ...
        Stream : 0x080 ::$DATA
        Attributes : 0x00000000: *NONE*
        Flags : 0x00000000: *NONE*
        Size : 1,520
        Allocated Size : 4,096
        Extents : 1 Extents
        : 1: VCN: 0 Clusters: 1 LCN: 1,497,204

有关更多信息,您可以阅读以下问题

关于java - 是否有 GetCompressedFileSize 的 Java 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14735485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com