Welcome, guest | Sign In | My Account | Store | Cart

This program is meant to be used to extract *.BMP and *.JPG files from one file that has one or more of these image files stored inside of it. Corruption of the image file is verified manually after the file has been extracted.

Python, 183 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
bmp_magic_a = 'BM'
jpg_magic_a = '\xFF\xD8\xFF\xE0'
jpg_magic_z = '\xFF\xD9'

def main():
    welcome()
    while True:
        main_menu()
        if main_query() == 1:
            workspace()
        else:
            print
            break

def welcome():
    print
    print '  Welcome to'
    print 'FILE EXTRACTOR'
    print '=============='
    print

def main_menu():
    print '(1) Open Workspace'
    print '(2) Quit Application'

def main_query():
    while True:
        try:
            select = int(raw_input('Select: '))
            if 0 < select < 3:
                return select
            else:
                print 'ERROR: Selection Must Be 1 Or 2'
        except Exception, error:
            if error.__class__ is EOFError:
                print
            print 'ERROR: Selection Must Be A Number'

def workspace():
    try:
        data = get_file()
        while True:
            work_menu()
            select = work_query()
            if select == 1:
                extract_bmp(data)
            elif select == 2:
                extract_jpg(data)
            elif select == 3:
                break
            else:
                raise SystemExit
    except Exception, error:
        if error.__class__ is SystemExit:
            raise SystemExit
        else:
            print

def get_file():
    while True:
        try:
            return file(raw_input('Filename: '), 'rb', 0).read()
        except Exception, error:
            if error.__class__ is EOFError:
                raise EOFError
            else:
                print 'ERROR: Filename Is Invalid'

def work_menu():
    print '(1) Extract *.BMP'
    print '(2) Extract *.JPG'
    print '(3) Back'
    print '(4) Exit'

def work_query():
    while True:
        try:
            select = int(raw_input('Select: '))
            if 0 < select < 5:
                return select
            else:
                print 'ERROR: Selection Must Be Between 1 And 4'
        except Exception, error:
            if error.__class__ is EOFError:
                print
            print 'ERROR: Selection Must Be A Number'
            
def extract_bmp(data):
    extract(data, True)

def extract_jpg(data):
    extract(data, False)

def extract(data, bmp):
    while True:
        try:
            size = get_size()
            if bmp:
                file_list = search_bmp(data, size)
            else:
                file_list = search_jpg(data, size)
            if len(file_list) == 0:
                print 'ERROR: Cannot Find File'
            else:
                while len(file_list) != 0:
                    print len(file_list), 'files can be extracted.'
                    select = get_select(len(file_list))
                    save_point = get_save_point()
                    save_point.write(file_list[select - 1])
                    save_point.close()
                    del file_list[select - 1]
        except:
            print
            break

def get_size():
    while True:
        try:
            size = int(raw_input('File Size: '))
            if 0 < size < 4294967296:
                return size
            else:
                print 'ERROR: Size Must Be Between 1 And 4294967295'
        except Exception, error:
            if error.__class__ is EOFError:
                raise EOFError
            else:
                print 'ERROR: Size Must Be A Number'

def search_bmp(data, size):
    global bmp_magic_a
    file_list = list()
    search_string = bmp_magic_a \
    + chr((size & 0xFF) >> (8 * 0)) \
    + chr((size & 0xFF00) >> (8 * 1)) \
    + chr((size & 0xFF0000) >> (8 * 2)) \
    + chr((size & 0xFF000000) >> (8 * 3))
    index = data.find(search_string)
    while index != -1:
        file_list.append(data[index:index+size])
        index = data.find(search_string, index + 1)
    while len(file_list) != 0 and len(file_list[-1]) != size:
        del file_list[-1]
    return file_list

def search_jpg(data, size):
    global jpg_magic_a, jpg_magic_z
    file_list = list()
    index_1 = data.find(jpg_magic_a)
    index_2 = data.find(jpg_magic_z, index_1)
    while index_1 != -1 and index_2 != -1:
        if index_2 - index_1 == size - len(jpg_magic_z):
            file_list.append(data[index_1:index_1+size])
        index_1 = data.find(jpg_magic_a, index_1 + 1)
        index_2 = data.find(jpg_magic_z, index_1)
    return file_list

def get_select(numbers):
    while True:
        try:
            select = int(raw_input('Extract: '))
            if 1 <= select <= numbers:
                return select
            else:
                print 'ERROR: Must Be Between 1 And', numbers
        except Exception, error:
            if error.__class__ is EOFError:
                raise EOFError
            else:
                print 'ERROR: Must Be A Number'

def get_save_point():
    while True:
        try:
            return open(raw_input('Save To: '), 'wb', 0)
        except Exception, error:
            if error.__class__ == EOFError:
                raise EOFError
            else:
                print 'ERROR: Filename Is Invalid'

if __name__ == '__main__':
    main()

This program was designed for use with a deleted-file recovery program. The recovery program would old save the pictures in its special format and would not extract them. This program helped to get the files out quite easily.

Created by Stephen Chappell on Mon, 21 Nov 2005 (PSF)
Python recipes (4591)
Stephen Chappell's recipes (233)

Required Modules

  • (none specified)

Other Information and Tasks