Ejemplo n.º 1
0
class RarFileExtractor(Extractor):
    def __init__(self, archive_path: str):
        self.archive_path = archive_path
        self.rarfile = RarFile(self.archive_path)
        self.namelist = {
            i.filename
            for i in self.rarfile.infolist() if not i.isdir()
        }

    def __exit__(self, *args):
        self.rarfile.close()

    def list_files(self) -> {}:
        return self.namelist

    def extract(self,
                file_name: str,
                dest_path: str,
                write_hook: Callable[[bytes], bytes] = None):
        with self.rarfile.open(file_name) as rf, open(dest_path, 'wb') as out:
            while True:
                chunk = rf.read(CHUNKSIZE)
                if not chunk:
                    break
                out.write(write_hook(chunk) if write_hook else chunk)
Ejemplo n.º 2
0
    def save_file(self, datafile):
        """
        Ouvrir le fichier uploadé et créer les images contenues

        :param datafile: nom du fichier d'archive ou handle de fichier
        """
        if isinstance(datafile, str):
            datafile = open(datafile, 'r')
        content_type = datafile.content_type
        if content_type in {
                'application/zip', 'application/x-zip-compressed',
                'application/x-rar-compressed'
        }:
            if content_type in {'application/x-rar-compressed'}:
                archive = RarFile(datafile, 'r')
            else:
                archive = ZipFile(datafile, 'r')
            names = archive.namelist()
            for name in names:
                filename, fileext = os.path.splitext(name.lower())
                if fileext in ('.png', '.jpg', '.jpeg'):
                    item = archive.open(name)
                    with NamedTemporaryFile(prefix=slugify(filename),
                                            suffix=fileext,
                                            delete=False) as tfile:
                        tfile.write(item.read())
                        picture = Picture(author=self.request.user)
                        picture.image.save(tfile.name, File(tfile))
                        picture.save()
                    item.close()
            archive.close()
            datafile.close()
            return self.cleaned_data
        else:
            raise forms.ValidationError(_("File must be a zip or rar file"))
Ejemplo n.º 3
0
 def __init__(self, studentnr, specs, source_filename):
     Submission.__init__(self, studentnr)
     try:
         source_file = RarFile(source_filename)
         for spec in specs:
             part = SubmissionPart(spec)
             self.parts.append(part)
             for filename in source_file.namelist():
                 filename = filename.replace('\\', '/')
                 info = source_file.getinfo(filename)
                 if not info.isdir() and spec.matches(filename):
                     part.add_data(filename, source_file.open(filename).read())
     except BadRarFile:
         pass
     except NotRarFile:
         pass
Ejemplo n.º 4
0
    while True:
        gevent.sleep(0)
        chunk = src.read(chunk_size)
        if not chunk:
            break
        dst.write(chunk) 
        total_copied += len(chunk)

        progress_cb(total_copied)


if __name__ == "__main__":
    rf = RarFile('/media/zuhair/Shuttle/Legion.S02E01.1080p.WEB.H264-DEFLATE/legion.s02e01.1080p.web.h264-deflate.rar')
    info = rf.infolist()[0]
    src = rf.open(info, 'r')
    dst = open('/tmp/legion.mkv', mode='wb')

    progress_queue = Queue(maxsize=1)

    def on_progress(copied):
        try:
            progress_queue.put_nowait(copied)
        except QueueFullException:
            # Swallow the exception and go on
            pass
    
    def printer():
        start_timestamp = time.time()
        loop_timestamp = start_timestamp
        loop_progress = 0