1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

最近、仕事でzipファイルを扱っていたのですが、ファイルが破損していたためファイルの一部が破損していました。
なにか、zipファイルを復元する方法がないか調べていたら、復元するコマンドを見つけたので今回実践していこうと思います。

zipファイルを破損させて、復元する

zipファイルを破損させる

まず、破損させたzipファイルを作成します。
sample.txtというファイルを作成します。
image.png

zipコマンドでファイルをzipに圧縮します。

 zip sample.zip sample.txt

image.png

それでは、zipファイルを破損させたいと思います。
zipファイルを破損させる方法は色々ありますが、今回はhexeditコマンドを用いて、バイナリファイルを編集します。
hexeditはファイルを16進数(Hex)の形式で表示し、編集することができます。

以下のコマンドを実行します。

hexedit sample.zip

すると、下のようにバイナリデータを編集することができます。
image.png

最後の00 00 00 00 00FFに置き換えます。(FFと入力すれば置き換わります)
※編集する場所によっては修復不可の場合もあります。今回は大丈夫な部分です。
image.png
Ctrl + Xを押して、yでsaveします。

unzipコマンドでsample.zipを解凍してみます。

$ unzip sample.zip
Archive:  sample.zip

caution:  zipfile comment truncated
error [sample.zip]:  missing 4294967040 bytes in zipfile
  (attempting to process anyway)
error: invalid zip file with overlapped components (possible zip bomb)

エラーが出て解凍できないことが確認できます。

zipファイルを復元する

zipコマンドで-FFというオプションを指定してファイルを修復します。

$ zip -FF sample.zip --out repaired.zip
Fix archive (-FF) - salvage what can
        zip warning: zipfile comment truncated - ignoring
 Found end record (EOCDR) - says expect single disk archive
Scanning for entries...
 copying: sample.txt  (19 bytes)
Central Directory found...
EOCDR found ( 1    167)...

実行すると、repaired.zipというファイルが作成されます。

repaired.zipを解凍してみます。
renameでrepaired_sample.txtという名前のファイルに解凍します。

$ unzip repaired.zip
Archive:  repaired.zip

caution:  zipfile comment truncated
replace sample.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: r
new name: repaired_sample.txt
 extracting: repaired_sample.txt

ファイル内容も破損なく、復元されているのが確認できます。
image.png

終わりに

今回は復元可能な部分を編集し破損させることで、zipファイルを破損状態から復元することができました。
zipファイルが破損してしまっている場合は今回のようにやれば復元できるかもしれません。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?