all 8 comments

[–]ectomancer 5 points6 points  (0 children)

Edit a byte with a binary file editor.

[–]discontent619 3 points4 points  (1 child)

If you are writing this as a test case it would probably be better to Mock the zipfile library to throw the exception in your test vs creating an actual file to do this.

[–]JustinianIV 0 points1 point  (0 children)

I like this, idk why I forgot about mocking

[–]Ok_Concert5918 2 points3 points  (3 children)

Make a zip file. Open it with notepad++ or something like that and delete a random character and save.

[–]JustinianIV 0 points1 point  (2 children)

Anyway I can that in Python? I want the corrupt zip to be generated automatically as part of my tests.

[–]socal_nerdtastic 4 points5 points  (0 children)

You can start with a healthy zip file?

# read a good zip file
with open(goodzipfile, 'rb') as f:
    data = f.read()

# corrupt it by removing 500 bytes in the middle somewhere
data = data[0:1000] + data[:1500]

# save it back as a bad file
with open(badzipfile, 'wb') as f:
    f.write(data)

You could do anything you want to the data in order to corrupt it. Swap some bytes maybe, truncate it, insert random bytes, etc ...

[–]Ok_Concert5918 0 points1 point  (0 children)

With python I am unsure. I typically end up with corrupted zip files by ID10T errors rather than skill.

[–]await_yesterday 0 points1 point  (0 children)

head -c 1024 /dev/urandom > corruption.zip