如何在vim中获取gunzipped文件的大小

时间:2009-01-08 21:36:05

标签: vim gzip

查看(或编辑).gz文件时,vim知道找到gunzip并正确显示文件。
在这种情况下,getfsize(expand(“%”))将是gzip压缩文件的大小。

有没有办法获得扩展文件的大小?

[编辑]
解决这个问题的另一种方法可能是获取当前缓冲区的大小,但在vim中似乎没有这样的函数。我错过了什么吗?

4 个答案:

答案 0 :(得分:1)

没有简单的方法来获取压缩文件的未压缩大小,缺少解压缩和使用getfsize()函数。那可能不是你想要的。我看了RFC 1952 - GZIP File Format Specification,唯一可能有用的是ISIZE字段,它包含“......原始(未压缩)输入数据模2 ^ 32的大小”。

编辑:

我不知道这是否有帮助,但是这里有一些概念验证C代码,我将它们放在一起,检索gzip文件中ISIZE字段的值。它适用于我使用Linux和gcc,但你的里程可能会有所不同。如果您编译代码,然后传入一个gzip文件名作为参数,它将告诉您原始文件的未压缩大小。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[])
{
    FILE *fp = NULL;
    int  i=0;

    if ( argc != 2 ) {
        fprintf(stderr, "Must specify file to process.\n" );
        return -1;
    }

    // Open the file for reading
    if (( fp = fopen( argv[1], "r" )) == NULL ) {
        fprintf( stderr, "Unable to open %s for reading:  %s\n", argv[1], strerror(errno));
        return -1;
    }

    // Look at the first two bytes and make sure it's a gzip file
    int c1 = fgetc(fp);
    int c2 = fgetc(fp);
    if ( c1 != 0x1f || c2 != 0x8b ) {
        fprintf( stderr, "File is not a gzipped file.\n" );
        return -1;
    }


    // Seek to four bytes from the end of the file
    fseek(fp, -4L, SEEK_END);

    // Array containing the last four bytes
    unsigned char read[4];

    for (i=0; i<4; ++i ) {
        int charRead = 0;
        if ((charRead = fgetc(fp)) == EOF ) {
            // This shouldn't happen
            fprintf( stderr, "Read end-of-file" );
            exit(1);
        }
        else
            read[i] = (unsigned char)charRead;
    }

    // Copy the last four bytes into an int.  This could also be done
    // using a union.
    int intval = 0;
    memcpy( &intval, &read, 4 );

    printf( "The uncompressed filesize was %d bytes (0x%02x hex)\n", intval, intval );

    fclose(fp);

    return 0;
}

答案 1 :(得分:1)

这似乎可以用于获取缓冲区的字节数

(line2byte(line("$")+1)-1)

答案 2 :(得分:0)

如果您使用的是Unix / Linux,请尝试

:%!wc -c 

那是以字节为单位的。 (如果您安装了例如cygwin,它适用于Windows。)然后点击u以恢复您的内容。

HTH

答案 3 :(得分:0)

在vim编辑器中,试试这个:

<Esc>:!wc -c my_zip_file.gz

这将显示文件所具有的字节数。

相关问题