Article Title: Garbled characters in Japanese compressed files. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
I used a Japanese linux system. I collected a compressed file from my colleague yesterday. After decompression, the file name is messy (the content is displayed normally ). Google found the following on the internet because the default encoding method in windows is Shift-JIS. in linux, although the content can be displayed, the file name is garbled. The solution is quite simple. perl provides some methods to directly write a short program.
I will also give an example program for reference from other places. (The Archive: Zip module must be installed here)
#! /Usr/bin/perl
Use strict;
Use warnings;
Use Archive: Zip qw (: ERROR_CODES: CONSTANTS );
Use Encode qw/from_to /;
Use Getopt: Long;
Use DateTime;
Use DateTime: TimeZone: Local;
GetOptions ('list | L' => \ my $ mode_list );
If (scalar @ ARGV <1 ){
Print "Usage: $0 [-l] \ N ";
Exit 1;
}
My $ zipfile = shift @ ARGV;
My $ zip = Archive: Zip-> new ();
Unless ($ zip-> read ($ zipfile) = AZ_ OK ){
Die 'read error ';
}
Print "Archive: $ zipfile \ n ";
If ($ mode_list ){
Print < < p><>
Length Date Time Name
---------------------------
HERE
}
My $ total_size = 0;
My @ members = $ zip-> members ();
My $ members_count = scalar @ members;
Foreach my $ member (@ members ){
My $ filename = $ member-> fileName;
From_to ($ filename, 'cp932', 'utf8 ');
If ($ mode_list ){
My $ dt = DateTime-> from_epoch (
Epoch => $ member-> lastModTime
)-> Set_time_zone (DateTime: TimeZone: Local-> TimeZone ());
$ Total_size + = $ member-> uncompressedSize;
Printf "% 9d % s \ n ",
$ Member-> uncompressedSize,
$ Dt-> ymd ('-'). ''. sprintf ("% 02d", $ dt-> hour ). ':'. sprintf ("% 02d", $ dt-> min ),
$ Filename;
} Else {
Print "inflating: $ filename \ n ";
Unless (defined $ zip-> extractMember ($ member, $ filename )){
Warn "failed: $ filename \ n ";
}
}
}
My $ members_count_str = "$ members_count file". ($ members_count> 1? 'S ':'');
If ($ mode_list ){
Print < < p><>
---------------
HERE
Printf "% 9d % s \ n", $ total_size, $ members_count_str;
}