Python Forum
how many bytes in a file before zero padding
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how many bytes in a file before zero padding
#5
(Feb-02-2019, 10:13 AM)Larz60+ Wrote:
>>> from pathlib import Path
>>> home = Path('.')
>>> Noc = home / 'NocturneinC-sharpMinorFredericChopinPiano.mp4'
>>> Noc.stat().st_size
9017139
>>>
that's how big the file is. now run a script that generates a random number from 64 to 1048576 (not necessarily a power of 2). append a random number of zero-bytes to the file. the file is now larger but the data is still all there. now the script i need to make would be able to determine the approximate size of the data (exact size if the data happened to end in a non-zero byte).

suppose the original file's last byte is 0x46. suppose the random number happens to be 5417. there will be 5417 bytes appended, changing the above results to 9022556. the length of the file will be 9022556 but the length to be determined is 9017139. 9017139 is the length of the data. the posix command truncate -s 9017139 ... applied to the appended file would be able to restore the file to its original size. but you need to know the value 9017139. if all you get is the file with appended zero bytes, you won't know until you read the file contents and look for that last non-zero byte. the program i need to create is going to determine this for all the files but not (yet) truncate any of them.

if any of the original files happen to end in a zero byte (0x00) or a run of them, then the original size cannot be determined. but it is known that there will be a non-zero byte in the last 64 bytes. the units of this data are not larger than 64 and have a non-zero length prefix (e.g. its length counts itself).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
RE: how many bytes in a file before zero padding - by Skaperen - Feb-03-2019, 12:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Get amount of bytes in a file chesschaser 1 1,544 Aug-23-2021, 03:24 PM
Last Post: deanhystad
  replace bytes with other byte or bytes BigOldArt 1 10,527 Feb-02-2019, 11:00 PM
Last Post: snippsat
  how to read a text file as bytes Skaperen 1 8,053 May-29-2018, 08:12 AM
Last Post: killerrex
  Aes padding Hairy_Ape 1 2,297 May-23-2018, 12:05 AM
Last Post: scidam
  Correct way to change bytes in a file? Raptor88 16 26,234 Feb-23-2017, 06:08 PM
Last Post: Raptor88

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020