예제 #1
0
    def test_crc16(self):
        filepath = os.path.join(self.tmpdir, 'test.gz')

        with GzipFile.open(filepath, mode='w') as gzip:
            gzip.addfile(self.TEST_FILE, crc16=True)

        with GzipFile.open(filepath, mode='r') as gzip:
            info = gzip.getinfo(self.FILE_NAME)
            self.assertIsNotNone(info.CRC16)
예제 #2
0
    def test_ascii(self):
        filepath = os.path.join(self.tmpdir, 'test.gz')

        with GzipFile.open(filepath, mode='w') as gzip:
            gzip.addfile(self.TEST_FILE, isascii=True)

        with GzipFile.open(filepath, mode='r') as gzip:
            info = gzip.getinfo(self.FILE_NAME)
            self.assertTrue(info.FLG & FTEXT > 0)
예제 #3
0
    def test_best_speed(self):
        filepath = os.path.join(self.tmpdir, 'test.gz')

        with GzipFile.open(filepath, mode='w') as gzip:
            gzip.addfile(self.TEST_FILE, compresslevel=1)

        with GzipFile.open(filepath, mode='r') as gzip:
            info = gzip.getinfo(self.FILE_NAME)
            self.assertEqual(info.XFL, 4)
예제 #4
0
    def test_file_comment(self):
        filepath = os.path.join(self.tmpdir, 'test.gz')
        comment = 'onion, shallot and garlic'

        with GzipFile.open(filepath, mode='w') as gzip:
            gzip.addfile(self.TEST_FILE, comment=comment)

        with GzipFile.open(filepath, mode='r') as gzip:
            info = gzip.getinfo(self.FILE_NAME)
            self.assertEqual(comment, info.FCOMMENT)
예제 #5
0
    def test_exfield(self):
        filepath = os.path.join(self.tmpdir, 'test.gz')
        exfield = b'cp\x02\x08\x00'

        with GzipFile.open(filepath, mode='w') as gzip:
            gzip.addfile(self.TEST_FILE, exfield=exfield)

        with GzipFile.open(filepath, mode='r') as gzip:
            info = gzip.getinfo(self.FILE_NAME)
            self.assertEqual(exfield, info.EXFIELD)
예제 #6
0
    def test_write_contents(self):
        filepath = os.path.join(self.tmpdir, 'test.gz')

        with GzipFile.open(filepath, mode='w') as gzip:
            gzip.addfile(self.TEST_FILE)

        with GzipFile.open(filepath, mode='r') as gzip, \
             open(self.TEST_FILE, mode='rb') as orig:

            fp = gzip.extract(self.FILE_NAME)
            self.assertEqual(fp.read(), orig.read())
예제 #7
0
    def test_write_attributes(self):
        filepath = os.path.join(self.tmpdir, 'test.gz')

        with GzipFile.open(filepath, mode='w') as gzip:
            gzip.addfile(self.TEST_FILE)

        with GzipFile.open(filepath, mode='r') as gzip:
            info = gzip.getinfo(self.FILE_NAME)

            self.assertEqual(info.CM, 8)
            self.assertEqual(info.FLG, 0b00001000)
            self.assertEqual(info.MTIME, int(os.path.getmtime(self.TEST_FILE)))
            self.assertEqual(info.XFL, 0)
            self.assertEqual(info.FNAME, os.path.basename(self.TEST_FILE))
예제 #8
0
    def test_write_data(self):
        filepath = os.path.join(self.tmpdir, 'test.gz')
        data = b'carrot, beet, radish and turnip'
        mtime = 1412132400 # 2014-10-01 12:00:00
        filename = 'roots_vagetables.txt'

        with GzipFile.open(filepath, mode='w') as gzip:
            gzip.adddata(data, mtime=mtime, filename=filename)

        with GzipFile.open(filepath, mode='r') as gzip:
            info = gzip.getinfo(filename)

            self.assertEqual(mtime, info.MTIME)
            self.assertEqual(filename, info.FNAME)

            fp = gzip.extract(gzipinfo=info)
            self.assertEqual(data, fp.read())
예제 #9
0
    def test_extract_file(self):
        tmpdir = None
        try:
            tmpdir = tempfile.mkdtemp()
            os.chdir(tmpdir)

            with GzipFile.open(self.TEST_FILE) as gzip:
                filename = self.FILE_ATTR['FNAME']
                gzip.extractfile(filename)

                stat = os.stat(filename)
                self.assertEqual(stat.st_mtime, self.FILE_ATTR['MTIME'])
                self.assertEqual(stat.st_size, len(self.FILE_CONTENTS))
        finally:
            if tmpdir:
                shutil.rmtree(tmpdir)
예제 #10
0
    def test_extract_file(self):
        tmpdir = None
        try:
            tmpdir = tempfile.mkdtemp()
            os.chdir(tmpdir)

            with GzipFile.open(self.TEST_FILE) as gzip:
                filename = self.FILE_ATTR['FNAME']
                gzip.extractfile(filename)

                stat = os.stat(filename)
                self.assertEqual(stat.st_mtime, self.FILE_ATTR['MTIME'])
                self.assertEqual(stat.st_size, len(self.FILE_CONTENTS))
        finally:
            if tmpdir:
                shutil.rmtree(tmpdir)
예제 #11
0
 def test_extra_bytes(self):
     with GzipFile.open(self.EXBYTES_FILE) as gzip:
         self.assertEqual(len(gzip.gzipinfos), 1)
예제 #12
0
 def test_read_emptyfile(self):
     with self.assertRaises(IOError):
         with GzipFile.open(self.EMPTY_FILE) as gzip:
             pass
예제 #13
0
 def test_read_contents(self):
     with GzipFile.open(self.TEST_FILE) as gzip:
         fp = gzip.extract(self.FILE_ATTR['FNAME'])
         self.assertEqual(fp.read(), self.FILE_CONTENTS)
예제 #14
0
 def test_load_file(self):
     with GzipFile.open(self.TEST_FILE) as gzip:
         self.assertEqual(len(gzip.gzipinfos), 1)
예제 #15
0
 def test_extra_bytes(self):
     with GzipFile.open(self.EXBYTES_FILE) as gzip:
         self.assertEqual(len(gzip.gzipinfos), 1)
예제 #16
0
 def test_bad_crc16(self):
     with self.assertRaises(BadChecksum):
         with GzipFile.open(self.CRC16_FILE) as gzip:
             pass
예제 #17
0
 def test_read_multiple(self):
     with GzipFile.open(self.MULTIPLE_FILE) as gzip:
         infos = gzip.getinfolist()
         self.assertEqual(len(infos), 2)
예제 #18
0
 def test_read_contents(self):
     with GzipFile.open(self.TEST_FILE) as gzip:
         fp = gzip.extract(self.FILE_ATTR['FNAME'])
         self.assertEqual(fp.read(), self.FILE_CONTENTS)
예제 #19
0
    def test_check_attributes(self):
        with GzipFile.open(self.TEST_FILE) as gzip:
            info = gzip.gzipinfos[0]

            for key in self.FILE_ATTR:
                self.assertEqual(getattr(info, key), self.FILE_ATTR[key])
예제 #20
0
 def test_extra_magic(self):
     with self.assertRaises(GzipError):
         with GzipFile.open(self.EXMAGIC_FILE) as gzip:
             pass
예제 #21
0
 def test_bad_isize(self):
     with self.assertRaises(BadChecksum):
         with GzipFile.open(self.ISIZE_FILE) as gzip:
             pass
예제 #22
0
 def test_bad_isize(self):
     with self.assertRaises(BadChecksum):
         with GzipFile.open(self.ISIZE_FILE) as gzip:
             pass
예제 #23
0
 def test_read_emptyfile(self):
     with self.assertRaises(IOError):
         with GzipFile.open(self.EMPTY_FILE) as gzip:
             pass
예제 #24
0
    def test_check_attributes(self):
        with GzipFile.open(self.TEST_FILE) as gzip:
            info = gzip.gzipinfos[0]

            for key in self.FILE_ATTR:
                self.assertEqual(getattr(info, key), self.FILE_ATTR[key])
예제 #25
0
 def test_extra_magic(self):
     with self.assertRaises(GzipError):
         with GzipFile.open(self.EXMAGIC_FILE) as gzip:
             pass
예제 #26
0
 def test_read_multiple(self):
     with GzipFile.open(self.MULTIPLE_FILE) as gzip:
         infos = gzip.getinfolist()
         self.assertEqual(len(infos), 2)
예제 #27
0
 def test_bad_crc16(self):
     with self.assertRaises(BadChecksum):
         with GzipFile.open(self.CRC16_FILE) as gzip:
             pass
예제 #28
0
 def test_load_file(self):
     with GzipFile.open(self.TEST_FILE) as gzip:
         self.assertEqual(len(gzip.gzipinfos), 1)