<commit_before>import os
import tempfile
import shutil
from zipfile import ZipFile


class ZIPHandler(object):
    def walk(self, archive):
        with ZipFile(archive, 'r') as zip:
            for name in zip.namelist():
                if not name.endswith('/'):
                    contents = zip.read(name)
                    yield name, contents, None

    def get(self, archive, name):
        with ZipFile(archive, 'r') as zip:
            return zip.read(name)

    def set(self, archive, name, contents, mode=None):
        d = os.path.dirname(archive)
        if d and not os.path.exists(d):
            os.makedirs(d)

        with ZipFile(archive, 'a') as zip:
            zip.writestr(name, str(contents))

    def delete(self, archive, name):
        fd, tmp = tempfile.mkstemp()
        try:
            shutil.copy(archive, tmp)
            with ZipFile(tmp, 'r') as old_zip:
                with ZipFile(archive, 'w') as new_zip:
                    for member in old_zip.namelist():
                        if member != name:
                            new_zip.writestr(member, old_zip.read(member))
        except:
            os.close(fd)
            os.unlink(tmp)<commit_msg>Fixed two more problems with the ziphandler.<commit_after>import os
import tempfile
import shutil
from zipfile import ZipFile
from pixiepatch import netpath


class ZIPHandler(object):
    def walk(self, archive):
        with ZipFile(archive, 'r') as zip:
            for name in zip.namelist():
                if not name.endswith('/'):
                    contents = zip.read(name)
                    yield name, contents, None

    def get(self, archive, name):
        with ZipFile(archive, 'r') as zip:
            return zip.read(netpath(name))

    def set(self, archive, name, contents, mode=None):
        d = os.path.dirname(archive)
        if d and not os.path.exists(d):
            os.makedirs(d)

        if os.path.exists(archive):
            with ZipFile(archive, 'r') as zip:
                delete = netpath(name) in zip.namelist()
            if delete:
                self.delete(archive, name)

        with ZipFile(archive, 'a') as zip:
            zip.writestr(netpath(name), str(contents))

    def delete(self, archive, name):
        name = netpath(name)
        fd, tmp = tempfile.mkstemp()
        try:
            shutil.copy(archive, tmp)
            with ZipFile(tmp, 'r') as old_zip:
                with ZipFile(archive, 'w') as new_zip:
                    for member in old_zip.namelist():
                        if member != name:
                            new_zip.writestr(member, old_zip.read(member))
        finally:
            os.close(fd)
            os.unlink(tmp)
{'fraction_non_alphanumeric': 0.10526315789473684, 'fraction_numerical': 0.0, 'mean_word_length': 1.7289156626506024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6666805', 'n_tokens_mistral': 812, 'n_tokens_neox': 769, 'n_words': 220}