Demo entry 1205555

A compression program

   

Submitted by Benjamin Tarr on Feb 03, 2015 at 05:32
Language: Python. Code size: 15.4 kB.

#!c:\python27

#imports
import sqlite3
import os
import sys
import time #for timestamp
import datetime #for timestamp
import wx #for wx python
import wx.media
import chardet # for unicode stuff
import binascii # for unicode stuff
#-------------------------Notes----------------------------------------
#Finish loading bar
#fix output dir
#try opening the file in binary and string all the charenc to open png files
#something wrong with the hexit if statement, could be that ASCII is not the correct string - line 305
#playAwsomeMusic

#-------------------------Wx Python Window-----------------------------
class windowClass(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(windowClass, self).__init__(*args, **kwargs)
        self.basicGui()
    def basicGui(self):
        # short for file dialog
        self.fd = ''
        self.fdo = ''
        self.fileRemoved = False
        
        Panel = wx.Panel(self)
        status=self.CreateStatusBar()

        # File Input Field
        StaticText1Id = wx.NewId()
        StaticText1 = wx.StaticText(Panel, StaticText1Id, "File: ", (5,7))
        self.textControl1 = wx.TextCtrl(Panel, pos=(30,6), size=(500,20),value='C:\\')

        fileInputButtonId = wx.NewId()
        fileInputButton = wx.Button(Panel,fileInputButtonId,'Browse',pos=(540,2))
        self.filePath = self.textControl1.GetValue()

        checkBox1Id = wx.NewId()
        self.checkBox1 = wx.CheckBox(Panel,checkBox1Id,"Overwrite original file?",(10,30),(500,-1))
        self.checkBox1.SetValue(False)

        checkBox2Id = wx.NewId()
        self.checkBox2 = wx.CheckBox(Panel,checkBox2Id,"Compress to different location/Change file name?",(10,50),(500,-1))
        self.checkBox2.SetValue(False)

        StaticText2Id = wx.NewId()
        StaticText2 = wx.StaticText(Panel, StaticText2Id, "New Location and/or filename: ", (10,73))
        self.textControl2 = wx.TextCtrl(Panel, pos=(178,72), size=(352,20),value='C:\\')
        self.textControl2.Enable(False)

        fileOutputButtonId = wx.NewId()
        self.fileOutputButton = wx.Button(Panel,fileOutputButtonId,'Browse',pos=(540,68))
        self.fileOutputButton.Enable(False)
        self.fileOutPath = self.textControl2.GetValue()

        compressButtonId = wx.NewId()
        self.compressButton = wx.Button(Panel,compressButtonId,'Compress',pos=(193,110))
        self.compressButton.Enable(False)

        decompressButtonId = wx.NewId()
        self.decompressButton = wx.Button(Panel,decompressButtonId,'Uncompress',pos=(360,110))
        self.decompressButton.Enable(False)

        # Loading Bar
        loadingBarId = wx.NewId()
        self.loadingBar = wx.Gauge(Panel,loadingBarId,100,(20,380),(595,20))

        loadingBarTextId = wx.NewId()
        loadingBarText = wx.StaticText(Panel,loadingBarTextId,'Progress',pos=(303,350))
        
        # the actual menu bar
        menuBar = wx.MenuBar()
        # the file menu
        fileButton = wx.Menu()
        editButton = wx.Menu()
        # the items in the menu bar
        openId = wx.NewId()
        self.openItem = wx.MenuItem(fileButton, openId, 'Open File...\tCtrl+O', 'Opens a file for compression/Uncompression.')
        fileButton.AppendItem(self.openItem)
        self.openItem.Enable(True)
        
        exitId = wx.NewId()
        self.exitItem = wx.MenuItem(fileButton, exitId, 'Quit\tCtrl+Q', 'Exits the application.')
        fileButton.AppendItem(self.exitItem)
        self.exitItem.Enable(True)

        compressId = wx.NewId()
        self.compressItem = wx.MenuItem(editButton, compressId, 'Compress\tCtrl+C', 'Compresses the opened file to a .b file.')
        editButton.AppendItem(self.compressItem)
        self.compressItem.Enable(False)
                               
        decompressId = wx.NewId()
        self.decompressItem = wx.MenuItem(editButton, decompressId, 'Uncompress\tCtrl+U','Uncompresses the opened file to its original state.')
        editButton.AppendItem(self.decompressItem)
        self.decompressItem.Enable(False)

        # Music
        musicPlayerId=wx.NewId()
        self.musicPlayer = wx.media.MediaCtrl(Panel,style=wx.SIMPLE_BORDER)
        self.musicPlayer.Load('C:\Python27\Python Programs\BenCompress Project\Final_Package\msc.mp3')
        
        # adding file button
        menuBar.Append(fileButton, '&File')
        # adding trans button
        menuBar.Append(editButton, '&Edit')
        # adding menu bar
        self.SetMenuBar(menuBar)

        # binding event to items
        #   Menu Items
        self.Bind(wx.EVT_MENU, self.OpenFile, self.openItem)
        self.Bind(wx.EVT_MENU, self.Quit, self.exitItem)

        self.Bind(wx.EVT_MENU, self.CompressionInit, self.compressItem)
        self.Bind(wx.EVT_MENU, self.DecompressionInit, self.decompressItem)
        #   Buttons
        self.Bind(wx.EVT_BUTTON, self.OpenFile, fileInputButton)
        self.Bind(wx.EVT_BUTTON, self.OpenLoc, self.fileOutputButton)

        self.Bind(wx.EVT_BUTTON, self.CompressionInit, self.compressButton)
        self.Bind(wx.EVT_BUTTON, self.DecompressionInit, self.decompressButton)
        #   Check Boxes
        self.Bind(wx.EVT_CHECKBOX, self.removeFirstFile, self.checkBox1)
        self.Bind(wx.EVT_CHECKBOX, self.OutputPathED, self.checkBox2)
        # Binding Music to music player
        self.Bind(wx.media.EVT_MEDIA_LOADED, self.PlayAwesomeMusic)
        

        # Default Stuff
        self.SetTitle('Bencompress')
        self.Show(True)
        self.SetSize((640,480))
        self.SetWindowStyle(wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN)
        self.Centre()
    def PlayAwesomeMusic(self, e):
        self.musicPlayer.Play()
    def OpenFile(self, e):
        fileDialog = wx.FileDialog(self, message="Open a file for Compression",defaultDir=os.getcwd(),
                                   defaultFile="", style=wx.OPEN)
        if fileDialog.ShowModal() == wx.ID_OK:
            self.fd = fileDialog
        
        if self.fd.GetFilename()[self.fd.GetFilename().find('.'):len(self.fd.GetFilename())] == '.b':
            self.decompressItem.Enable(True)
            self.compressItem.Enable(False)
            self.decompressButton.Enable(True)
            self.compressButton.Enable(False)
        else:
            self.compressItem.Enable(True)
            self.decompressItem.Enable(False)
            self.compressButton.Enable(True)
            self.decompressButton.Enable(False)
        self.textControl1.SetValue(self.fd.GetPath())
        self.textControl2.SetValue(self.fd.GetPath().replace(self.fd.GetFilename(),self.fd.GetFilename().replace(self.fd.GetFilename()[self.fd.GetFilename().find('.'):len(self.fd.GetFilename())],'.b')))
        self.fd.SetPath(self.textControl1.GetValue())
        self.fdo = self.fd
        self.fdo.SetPath(self.textControl2.GetValue())
    def OpenLoc(self, e):
        fileDialog2 = wx.FileDialog(self, message="Open a location to put the new file",defaultDir=os.getcwd(),
                                      defaultFile="",wildcard="All files(*.*)|*.*",style=wx.FD_SAVE)

        if fileDialog2.ShowModal() == wx.ID_OK:
            self.fdo = fileDialog2
        #print fileDialog2.GetPath()
    def CompressionInit(self, e):
        self.PlayAwesomeMusic(wx.media.EVT_MEDIA_LOADED)
        self.fd.SetPath(self.textControl1.GetValue())
        fd = self.fd
        fileStr = fd.GetPath()
        fileWholeName = fd.GetFilename()
        self.fdo.SetPath(self.textControl2.GetValue())
        CompressFile(fileStr,fileWholeName,self.textControl2.GetValue(),self.loadingBar)
        if self.fileRemoved == True:
            os.remove(fileStr)
            
        self.compressItem.Enable(False)
        self.compressButton.Enable(False)
        self.decompressItem.Enable(True)
        self.decompressButton.Enable(True)
        fileStr = fd.GetPath().replace(self.fd.GetFilename()[self.fd.GetFilename().find('.'):len(self.fd.GetFilename())],'.b')
        self.textControl2.SetValue(self.fd.GetPath())
        fd.SetPath(fileStr)
        self.fd = fd
        print 'it es doin it'
        self.textControl1.SetValue(self.fd.GetPath())
    def DecompressionInit(self, e):
        self.fdo.SetPath(self.textControl1.GetValue())
        DecompressFile(self.textControl1.GetValue(),self.textControl2.GetValue(),self.loadingBar)
        if self.fileRemoved == True:
            os.remove(self.textControl1.GetValue())
        self.compressItem.Enable(True)
        self.compressButton.Enable(True)
        self.decompressItem.Enable(False)
        self.decompressButton.Enable(False)
        tc1 = self.textControl1.GetValue()
        self.textControl1.SetValue(self.textControl2.GetValue())
        self.textControl2.SetValue(tc1)
    def OutputPathED(self, e):
        if self.textControl2.IsEnabled() == True:
            self.textControl2.Enable(False)
            self.fileOutputButton.Enable(False)
        else:
            self.textControl2.Enable(True)
            self.fileOutputButton.Enable(True)
    def removeFirstFile(self, e):
        if self.fileRemoved == False:
            self.fileRemoved = True
        else:
            self.fileRemoved = False
    def Quit(self, e):
        self.Close()

def main():
    app = wx.App()
    windowClass(None)
    app.MainLoop()

#-------------------------compressing the file-------------------------
def CompressFile(fileStr,fileWholeName,fileOutStr,loadingBar):
    # the file directory
    fileName = fileWholeName[0:fileStr.find('.')]
    fileExtension = fileWholeName[fileStr.find('.'):len(fileName)]
    # opening the file
    fileToCompress = open(fileStr, "r+b")
    fileData = b''
    k = 0
    for line in fileToCompress.readlines():
        fileData += line
        if k == 0:
            result = chardet.detect(line)
            charenc = result['encoding']
            k=1
    newData = fileData
    fileToCompress.close()

    # the start of the certain phrase being checked for repitions
    phraseStart = 0
    # the end of the certain phrase being checked for repitions
    phraseEnd = 1
    # the phrase to check for
    phrase = fileData[phraseStart:phraseEnd]
    # the array that holds all the past phrases to compare with eachother and see which is more efficient
    phrasePast = []
    # this array is used to transfer the used phrases over sqlite
    phrasesUsed = []

    # the key to the repitions, what they will be replaced with
    key_num = 0
    # a variable to exit the loop for a certain phrase when completion is at hand
    exit_loop = 0
    print "Start"
    while phraseStart<len(fileData):
        print str(phraseStart/(len(fileData))*100) + '% complete'
        print phraseStart
        print len(fileData)
        exit_loop = 0
        while exit_loop == 0:
            phrasePast.append(phrase)
            phraseEnd += 1
            if fileData.find(phrase,phraseEnd) != -1:
                phrase = fileData[phraseStart:phraseEnd]
            else:
                #for i in range(0,phraseEnd-phraseStart):
                    #if ( (fileData.count(phrasePast[i-1])*len(phrasePast[i-1]) ) < (fileData.count(phrase)*len(phrase)) ):
                        #print '1'+phrasePast[i-1]
                        #print '2'+phrasePast[i]
                        #print '3'+phrase
                        #phrase = phrasePast[i-1]
                        
                exit_loop = 1
        if len(str(key_num)) < len(phrase):
            print 'a'
            newData = newData.replace(phrase, "|"+"%X"%(key_num))
            print 'b'
            phrasesUsed.append(phrase)
            # adding 1 to the key
            key_num+=1
        # adding a phrase for sqlite transfer
        phraseStart = phraseEnd-1
        phraseEnd = phraseStart+1
        phrase = fileData[phraseStart:phraseEnd]
        phrasePast = []
        #update loading bar
        loadingBar.SetValue((phraseStart/len(fileData))*100)
        

    #print fileOutStr
    tempFile = open(fileOutStr,"wb")
    tempFile.write(newData)
    #print newData
    tempFile.close()
    #-------------------------Sqlite Tranfering to Server----------------------
    connection = sqlite3.connect("bencompress.db")
    connection.text_factory = str
    minion = connection.cursor()

    #timestamp = str(datetime.datetime.fromtimestamp(int(time.time())).strftime('%Y-%m-%d %H:%M:%S')
    if charenc == 'ascii':
        hexit = 0
    else:
        hexit = 1
    minion.execute("INSERT INTO FILES(FILE_NAME,COMPRESSION_TS,HEXIT_BOOL) VALUES (?,?,?)",
                   (fileOutStr,time.time(),hexit))
    connection.commit()
    # max is temperary, find a way to individualize files
    minion.execute("SELECT max(FILE_ID) FROM FILES WHERE FILE_NAME = ?",
                   (fileOutStr,))
    record = []
    record = minion.fetchall()
    fileId = int(list(record[0])[0])

    for k in range(0,len(phrasesUsed)):
        minion.execute("INSERT INTO FILE_KEYS(FILE_ID,KEY_STRING,KEY_INT) VALUES (?,?,?)",
                       (fileId,phrasesUsed[k],k))

    connection.commit()
    return charenc

#-------------------------Sqlite3 Variable Transfer------------------------
def DecompressFile(fileCompStr, finFileStr, loadingBar):
    connection = sqlite3.connect("bencompress.db")
    connection.text_factory = str
    minion = connection.cursor()
    minion.execute("SELECT max(FILE_ID) FROM FILES WHERE FILE_NAME = ?",
                        (fileCompStr,))
    record = []
    record = minion.fetchall()
    fileId = int(list(record[0])[0])
    #hexit check
    minion.execute("SELECT max(HEXIT_BOOL) FROM FILES WHERE FILE_NAME = ?",
                       (fileCompStr,))
    record = []
    record = minion.fetchall()
    hexit = int(list(record[0])[0])
    # hex the key string if an image
    if hexit == 1:
        # put in hex for key_stirng
        minion.execute("SELECT KEY_INT, hex(KEY_STRING) FROM FILE_KEYS WHERE FILE_ID = (SELECT max(FILE_ID) FROM FILES WHERE FILE_ID = ?)",
                            (fileId,))
    else:
        minion.execute("SELECT KEY_INT, KEY_STRING FROM FILE_KEYS WHERE FILE_ID = (SELECT max(FILE_ID) FROM FILES WHERE FILE_ID = ?)",
                            (fileId,))
    record = []
    record = minion.fetchall()
    fileKeys = list(record)

    #-------------------------Extracting the file------------------------------

    # the file directory
    fileToDecompress = open(fileCompStr,"r+b")
    # the data of the file
    fileCompData = fileToDecompress.read()
    fileToDecompress.close()
    
    for l in range(0,len(fileKeys)):
        fileKeys[l] = list(fileKeys[l])
        if hexit == 1:
            fileCompData = fileCompData.replace("|"+"%X"%(l),binascii.unhexlify(fileKeys[l][1]))
        else:
            fileCompData = fileCompData.replace("|"+"%X"%(l),fileKeys[l][1])
        loadingBar.SetValue((l/len(fileKeys))*100)
        
    finalFile = open(finFileStr, "wb")
    finalFile.write(fileCompData)
    finalFile.close()

main()

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).