Shrinking a file from start

  • Thread starter Thread starter EuroEager
  • Start date Start date
E

EuroEager

Guest
Assume a big file, almost occupying the only available drive on a machine.
This file consists of a big number of fixed size chunks.
The chunk size for all the records need to be increased thereby restructuring the file, I think there is no direct operations for doing this.

My first thought is a simple one: Read one by one of the chunks from the file and add them to a new file with extra bytes at the end of each chunk, but this requires that the drive must be able to store both the original file and the new file (which is bigger than the original file) simultaneously until all the write work has been done and then the original file may be deleted.

My second thought was to read the first chunk of the original file, add it with the spare bytes to the new file and delete the first chunk from the original file, doing this iteratively will then require a little more than the size of the new file because the original file is shrinked in parallel with extension of the new file (which is simple by calling e.g. FileStream.SetLength).

How can the original file be shrinked? Shrinking by cutting the end of the file is simple, just do FileStream.SetLength, but is it possible to do similar by cutting from the start of the file?
I could of course doing it all reversed by reading the last chunk, adding it to the new file, but then the new file must be increased by SetLength to expand the start of the file and this seems to be the same problem.

Please note that the drive is not able to hold both files (at full size) simultaneously.
Please don't tell me to add more hardware or replace the drive, the machine is only available remotely and physical access is not currently possible.

Continue reading...
 
Back
Top