예제 #1
0
 def _processing(self):
     self.pseudofile.seek(0)
     self.archive = py7zlib.Archive7z(self.pseudofile)
     if self.archive is not None and self.archive.getnames() is not None:
         logging.info("%s: Found a valid 7z archive" % self.name)
         for subfile in self.archive.getnames():
             self.unpacked_files[subfile] = None
             if self.password_protected and not self.password_found:
                 logging.info("%s: encrypted file '%s' and unable to find the password." % (self.name, subfile))
                 break
             try:
                 logging.info("%s: Trying to extract %s from archive" % (self.name, subfile))
                 self.unpacked_files[subfile] = StringIO.StringIO(self.archive.getmember(subfile).read())
                 logging.info("%s: successfully unpacked file '%s'" % (self.name, subfile))
             except py7zlib.NoPasswordGivenError as e:
                 self.password_protected = True
                 logging.info("%s: Archive is password protected" % self.name)
                 for pw in self.passwordlist:
                     try:
                         self.pseudofile.seek(0)
                         self.archive = py7zlib.Archive7z(self.pseudofile, password=pw)
                         self.unpacked_files[subfile] = self.archive.getmember(subfile).read()
                         self.password_found = True
                         logging.info("%s: found password: %s" % (self.name, pw))
                         break
                     except py7zlib.WrongPasswordError as e:
                         logging.info("%s: error: %s while trying password '%s'" % (self.name, e, pw))
                     except py7zlib.NoPasswordGivenError:
                         continue
                     except Exception as e:
                         raise ArchiveError(type(e))
             except Exception as e:
                 raise ArchiveError(type(e))
         self.pseudofile.close()
	def __init__(self, archPath=None, fileContents=None):

		self.archPath = archPath

		self.tempfile = None

		if fileContents:
			self.fType = magic.from_buffer(fileContents, mime=True).decode("ascii")
		elif archPath:
			self.fType = magic.from_file(archPath, mime=True).decode("ascii")
		else:
			raise NotAnArchive("You must pass either an archive file path or the contents of an\
				archive to the constructor!")

		if self.fType == 'application/x-rar':
			if fileContents:
				# Because rar is a shitty proprietary compression format,
				# it can't be used in normal libraries, and the legal
				# libraries only operate on real files. Therefore,
				# create a temp-file.
				self.tempfile = tempfile.NamedTemporaryFile()
				self.tempfile.write(fileContents)
				self.tempfile.flush()
				self.archPath = self.tempfile.name
			self.archHandle = rarfile.RarFile(self.archPath) # self._iterRarFiles()
			self.archType = "rar"
		elif self.fType == 'application/zip':
			try:
				if fileContents:  # Use pre-read fileContents whenever possible.
					self.archHandle = zipfile.ZipFile(io.BytesIO(fileContents))
				else:
					self.archHandle = zipfile.ZipFile(self.archPath) # self._iterZipFiles()

				self.archType = "zip"
			except zipfile.BadZipfile:
				raise CorruptArchive("File is not a valid zip archive!")

		elif self.fType == 'application/x-7z-compressed':
			try:
				if fileContents:  # Use pre-read fileContents whenever possible.
					self.archHandle = py7zlib.Archive7z(io.BytesIO(fileContents))
				else:
					self.fp = open(archPath, "rb")
					self.archHandle = py7zlib.Archive7z(self.fp) # self._iterZipFiles()

				self.archType = "7z"

			except py7zlib.ArchiveError:
				raise CorruptArchive("File is not a valid 7z archive!")
		else:
			raise NotAnArchive("Tried to create ArchiveReader on a non-archive file! File type: '%s'" % self.fType)
예제 #3
0
    def __init__(self, filedescriptor, archivename=None):
        super(Archive_7z, self).__init__(filedescriptor, archivename)
        self._fdescriptor = None
        try:
            self._handle = py7zlib.Archive7z(filedescriptor)
        except AttributeError:
            self._fdescriptor = open(filedescriptor, 'rb')
            self._handle = py7zlib.Archive7z(self._fdescriptor)

        if self._archivename is None:
            try:
                self._archivename = os.path.basename(str(filedescriptor))
            except Exception:
                self._archivename = "generic.7z"
예제 #4
0
def import_region(region):
    import shutil
    import tempfile
    import py7zlib
    import django.contrib.gis.gdal.error
    
    tempdir = tempfile.mkdtemp()
    filename = os.path.join(tempdir, 'region.7z')
    
    try:
        _download(region.shapefile_archive_url, filename)
        archive_file = open(filename, 'rb')
        archive = py7zlib.Archive7z(archive_file)

        # Тут импортируются данные
        imported_layers = [
                ('data/water-polygon', models.WaterModel, _add_water),
                ('data/vegetation-polygon', models.ForestModel, _add_forest),
                ('data/settlement-polygon', models.SettlementModel, _add_settlement),
                ('data/highway-line', models.HighwayModel, _add_highway),
                ('data/railway-station-point', models.RailwayStationModel, _add_railway_station)
        ]
        for imported_layer in imported_layers:
            _import_layer(region, archive, tempdir, imported_layer[0], imported_layer[1], imported_layer[2])
        _import_region_borders(region, archive, tempdir)
    except py7zlib.ArchiveError as e:
        raise ShapefileArchiveError(str(e))
    except django.contrib.gis.gdal.error.OGRException as e:
        raise ShapefileError(str(e))
    except django.contrib.gis.geos.error.GEOSException as e:
        raise ShapefileError(str(e))
    finally:
        shutil.rmtree(tempdir)
예제 #5
0
def extract(q_extract, q_insert):
    print("[*]\t[extract]: Waiting for archive to extract")
    sys.stdout.flush()

    while True:
        archive_name = q_extract.get()
        print("[*]\t[extract]: Extracting %s" % archive_name)
        sys.stdout.flush()

        fp = open(archive_name, "rb")
        archive = py7zlib.Archive7z(fp)

        # For each file in the archive
        for member in archive.getmembers():

            # Recover its content
            content = member.read()
            lines = content.split('\n')

            # Put each line of the current file in the insertion queue
            for line in lines:
                q_insert.put(line)

        # Close and delete the archive
        fp.close()
        os.remove(archive_name)
        q_extract.task_done()
        print("[*]\t[extract]: %s extraction done, %i to go" %
              (archive_name, q_extract.qsize()))
        sys.stdout.flush()
예제 #6
0
def main():
    #Get the arguments
    if (len(sys.argv) != 3):
        print("WizardImport <path of 7z file> <Mod name>")
        return
    wizard_file = sys.argv[1]
    mod_name = sys.argv[2]
    mod_dir_path = "[MOD] " + mod_name + "/"

    def renamePath(rename):
        rename = replaceStartIf(rename, "AA2_MAKE", "aa2edit/data")
        rename = replaceStartIf(rename, "AA2_PLAY", "aa2main/data")
        return rename

    try:
        fp = open(wizard_file, 'rb')
        archive = py7zlib.Archive7z(fp)
        for name, file in zip(archive.getnames(), archive.getmembers()):
            outputPath = mod_dir_path + renamePath(name)
            print(outputPath, "\t- ", file.size, " bytes")
            extractFile(outputPath, file)

        #TODO: rename PP folders to "[PP] <NAME>.pp" from "<NAME>"
        #TODO: Add fake "__HEADER" file

    finally:
        if fp:
            fp.close()
def getArchiveContent(fileName):

    fp = open(fileName, "rb")
    archive = py7zlib.Archive7z(fp)
    archiveMember = archive.getnames()[0]
    archiveContent = archive.getmember(archiveMember).read().decode()
    return archiveContent
    def __getArchives7z(self, filepath, archiveList, directory_to):
        try:
            import py7zlib
        except ImportError:
            # 32039 = Error launching .7z file.
            # 32129 = Please check kodi.log for details.
            message = "%s[CR]%s" % (util.localize(32039), util.localize(32129))
            xbmcgui.Dialog().ok(util.SCRIPTNAME, message)
            msg = (
                "You have tried to launch a .7z file but you are missing required libraries to extract the file. "
                "You can download the latest RCB version from RCBs project page. It contains all required libraries."
            )
            log.error(msg)
            return None

        fp = open(str(filepath), 'rb')
        archive = py7zlib.Archive7z(fp)
        archivesDecompressed = [(name, archive.getmember(name).read())
                                for name in archiveList]
        fp.close()

        if archivesDecompressed is None:
            log.warn("Error handling compressed file")
            return []
        for archive in archivesDecompressed:
            newPath = os.path.join(directory_to, archive[0])
            log.info("Putting extracted file in %s" % newPath)
            fo = open(str(newPath), 'wb')
            fo.write(archive[1])
            fo.close()

        return archivesDecompressed
예제 #9
0
    def __get_handle_7z(self, filename):
        if filename in self.handle:  # 이전에 열린 핸들이 존재하는가?
            zfile = self.handle.get(filename, None)
        else:
            zfile = py7zlib.Archive7z(open(filename, 'rb'))  # 7z 파일 열기
            self.handle[filename] = zfile

        return zfile
예제 #10
0
def readlog(path):

    with open(path, 'rb') as fp:
        archive = py7zlib.Archive7z(fp)
        for name in archive.getnames():
            print(name)
            log = archive.getmember(name).read()
    return log
예제 #11
0
def decompress():
    "Decompresses input/ztm_pack.7z and returns list of files"
    archive = py7zlib.Archive7z(open("input/ztm_pack.7z", "rb"))
    for name in archive.getnames():
        outfile = open(os.path.join("input", name), "wb")
        outfile.write(archive.getmember(name).read())
        outfile.close()
    return (archive.getnames())
예제 #12
0
 def check_file(self):
     """
     Проверка файла
     """
     try:
         with open(self.filepath, 'rb') as fp:
             py7zlib.Archive7z(fp)
             return True
     except:
         return False
예제 #13
0
 def is_7zfile(cls, file_path):
     is_7z = False
     fp = None
     try:
         fp = open(file_path, 'rb')
         archive = py7zlib.Archive7z(fp)
         n = len(archive.getnames())
         is_7z = True
     finally:
         if fp:
             fp.close()
     return is_7z
예제 #14
0
    def decompress(self, stream):
        if py7zlib is None:
            return Codec.decompress(self, stream)

        uncompressed = BytesIO()

        f = py7zlib.Archive7z(file=stream)
        for f_name in f.getnames():
            uncompressed.write(f.getmember(f_name).read())

        uncompressed.seek(0)
        return uncompressed
예제 #15
0
 def getTargetPath(self, localpath, name=None):
     import py7zlib
     try:
         zip = py7zlib.Archive7z(open(localpath, 'r'))
         members = zip.getnames()
         if len(members) > 0:
             if not name:
                 name = members[0]
             dir = os.path.dirname(localpath)
             return os.path.join(dir, name)
     except (IOError, OSError), e:
         raise Error, "%s: %s" % (localpath, e)
예제 #16
0
 def uncompress(self, localpath, name=None):
     import py7zlib
     try:
         zip = py7zlib.Archive7z(open(localpath, 'r'))
         members = zip.getnames()
         if not name:
             name = members[0]
         output = open(self.getTargetPath(localpath), "w")
         input = zip.getmember(name)
         data = input.read()
         output.write(data)
     except (IOError, OSError), e:
         raise Error, "%s: %s" % (localpath, e)
예제 #17
0
파일: skfileio.py 프로젝트: Rougnt/VNR-Core
def extract7z(path, location): # unicode, unicode -> bool
  """
  @param  path  unicode
  @param  location  unicode
  @return  bool
  """
  import py7zlib # could be found in pylzma from pip
  try:
    with open(path, 'rb') as fp:
      z = py7zlib.Archive7z(fp)
      extract7zarchive(z, location)
      return True
  except Exception, e:
    dwarn(e)
예제 #18
0
    def is_7zfile(cls, filepath):
        fp = None
        try:
            fp = open(filepath, 'rb')
            archive = py7zlib.Archive7z(fp)
            archive.getnames()
            is7z = True
        except Exception:
            is7z = False
        finally:
            if fp:
                fp.close()

        return is7z
예제 #19
0
 def _archive_handle(self, archive_type, payload):
     """get a handle for this archive type"""
     archive = None
     if archive_type == 'zip':
         if sys.version_info < (2, 7):
             payload = self._fix_python26_zipfile_bug(payload)
         archive = zipfile.ZipFile(payload)
     elif archive_type == 'rar':
         archive = rarfile.RarFile(payload)
     elif archive_type == 'tar':
         archive = tarfile.open(fileobj=payload)
     elif archive_type == '7z':
         archive = py7zlib.Archive7z(payload)
     return archive
예제 #20
0
 def __iter__(self):
     with open(self._path, "rb") as sz_file:
         archive = py7zlib.Archive7z(sz_file)
         for member in archive.files:
             if member.size > MAX_MEMORY_FILE_SIZE:
                 logging.warn("skipping %s in %s: too big (%d bytes)",
                              member.filename, self._path, member.size)
                 continue
             contents = StringIO(member.read())
             full_name = os.path.join(self._path, member.filename)
             try:
                 yield self._make_file_info(full_name, contents)
             except SkipROM:
                 pass
예제 #21
0
파일: launcher.py 프로젝트: roeiba/xbmc
def getArchives7z(filepath, archiveList):
	
	try:
		import py7zlib
	except:
		xbmcgui.Dialog().ok(util.SCRIPTNAME, util.localize(35039), util.localize(40029))
		Logutil.log("You have tried to launch a .7z file but you are missing required libraries to extract the file. You can download the latest RCB version from RCBs project page. It contains all required libraries.", util.LOG_LEVEL_ERROR)
		return None
	
	fp = open(str(filepath), 'rb')
	archive = py7zlib.Archive7z(fp)
	archivesDecompressed =  [(name, archive.getmember(name).read())for name in archiveList]
	fp.close()
	return archivesDecompressed
예제 #22
0
 def is_7zfile(cls, filepath):
     '''
     Class method: determine if file path points to a valid 7z archive.
     '''
     is7z = False
     fp = None
     try:
         fp = open(filepath, 'rb')
         archive = py7zlib.Archive7z(fp)
         n = len(archive.getnames())
         is7z = True
     finally:
         if fp:
             fp.close()
     return is7z
예제 #23
0
def is_7zfile(cls, filepath):
    '''
    Class method: determine if file path points to a valid 7z archive.
    '''
    is7z = False
    fp = None
    try:
        fp = open(filepath, 'r')
        archive = py7zlib.Archive7z(fp)
        n = len(archive.getnames())
        is7z = True
    except:
		if (is7z):
			zippedPath= os.path.join(zippedPath,archive)
			walktree(zippedPath, count, pathFolder)
    return is7z
예제 #24
0
def download_b3_file(filename: str) -> [str]:
    file_path = 'ftp://ftp.bmf.com.br/MarketData/Bovespa-Vista/{}'.format(
        filename)
    print('Downloading: {}'.format(file_path))
    with closing(request.urlopen(file_path)) as r:
        if '7z' in filename:
            archive = BytesIO(r.read())
            arquive_7z = py7zlib.Archive7z(archive)
            return decode(arquive_7z.getmember(0).read())
        elif 'gz' in filename:
            with gzip.open(r) as zip_file:
                return decode(zip_file.read())
        else:
            return decode(
                ZipFile(BytesIO(r.read())).read(
                    filename.replace('.zip', '.TXT')))
def extract7z(path: str):
    """
    Extracts 7z Archive

    :param path: The path of the archive
    :return:
    """

    with open(path, "rb") as in_file:
        arch = py7zlib.Archive7z(in_file)
        py7zlib.ArchiveFile

        for content in arch.getmembers():

            with open(content.filename, "wb") as out_file:
                out_file.write(content.read())
예제 #26
0
    def extractall(self, filepath, path):
        """
        Извлечение
        """
        self.filepath = filepath
        if not self.check_file():
            raise ArchiveError("Bad archive file. Can not open the file.")

        with open(self.filepath, "rb") as fp:
            archive = py7zlib.Archive7z(fp)
            for name in archive.getnames():
                outfilename = os.path.join(path, name)
                outdir = os.path.dirname(outfilename)
                if not os.path.exists(outdir):
                    os.makedirs(outdir)
                with open(outfilename, 'wb') as outfile:
                    outfile.write(archive.getmember(name).read())
예제 #27
0
 def content(self):
     if self.files:
         if self.main == "7z":
             with JoinFileOpener(*self.files) as f:
                 f7z = py7zlib.Archive7z(f)
                 name = f7z.getnames()[0]
                 self.file = ntpath.basename(name)
                 self.type = name.rsplit(".", 1)[-1].lower()
                 txt = f7z.getmember(name)
                 for l in io.StringIO(txt.read().decode()):
                     l = re_chomp.sub("", l)
                     yield l
         else:
             for file in self.files:
                 with open(file, "r") as f:
                     for l in f.readlines():
                         l = re_chomp.sub("", l)
                         yield l
예제 #28
0
    def __getArchives7z(self, filepath, archiveList):

        try:
            import py7zlib
        except ImportError:
            # 32039 = Error launching .7z file.
            # 32129 = Please check kodi.log for details.
            message = "%s[CR]%s" % (util.localize(32039), util.localize(32129))
            xbmcgui.Dialog().ok(util.SCRIPTNAME, message)
            msg = ("You have tried to launch a .7z file but you are missing required libraries to extract the file. "
                   "You can download the latest RCB version from RCBs project page. It contains all required libraries.")
            log.error(msg)
            return None

        fp = open(str(filepath), 'rb')
        archive = py7zlib.Archive7z(fp)
        archivesDecompressed = [(name, archive.getmember(name).read()) for name in archiveList]
        fp.close()
        return archivesDecompressed
예제 #29
0
    def __getNames7z(self, filepath):

        try:
            import py7zlib
        except ImportError as e:
            xbmcgui.Dialog().ok(util.SCRIPTNAME, util.localize(32039),
                                util.localize(32129))
            msg = (
                "You have tried to launch a .7z file but you are missing required libraries to extract the file. "
                "You can download the latest RCB version from RCBs project page. It contains all required libraries."
            )
            log.error(msg)
            log.error("Error: " + str(e))
            return None

        fp = open(str(filepath), 'rb')
        archive = py7zlib.Archive7z(fp)
        names = archive.getnames()
        fp.close()
        return names
예제 #30
0
def download_unpack_7z_to_catalog(url, catalog):
    """
    Funkcja pobiera plik *.7z z podanego adresu (url)
    i go rozpakowuje do podanego folderu (catalog)
    """
    path = os.path.join("..", "serwisy_xml", catalog)

    if not os.path.exists(path):
        os.makedirs(path)

    _, file = tempfile.mkstemp()
    urllib.request.urlretrieve(url, file)

    with open(file, 'rb') as f:
        archive = py7zlib.Archive7z(f)
        for name in archive.getnames():
            outfilename = os.path.join(path, name)
            outfile = open(outfilename, 'wb')
            outfile.write(archive.getmember(name).read())
            outfile.close()

    os.remove(file)