Sign in to follow this  
Followers 0
corgano

[s]Unzipping[/s] Zipping

39 posts in this topic

#1 ·  Posted (edited)

I made this post about unzipping .zip archives and Trancexx gave me the following code.

How would I modify this to zip a file? I know windows has "send to compressed folder" witch compresses it into a zip, but how would I do this in autoit?

Solved by Trancexx.

_ExtractZip("C:\Users\David\Desktop\New folder\Maps1.zip", "C:\Users\David\Desktop\New folder\maps")

; #FUNCTION# ;===============================================================================
;
; Name...........: _ExtractZip
; Description ...: Extracts file/folder from ZIP compressed file
; Syntax.........: _ExtractZip($sZipFile, $sDestinationFolder)
; Parameters ....: $sZipFile - full path to the ZIP file to process
;                  $sDestinationFolder - folder to extract to. Will be created if it does not exsist exist.
; Return values .: Success - Returns 1
;                          - Sets @error to 0
;                  Failure - Returns 0 sets @error:
;                  |1 - Shell Object creation failure
;                  |2 - Destination folder is unavailable
;                  |3 - Structure within ZIP file is wrong
;                  |4 - Specified file/folder to extract not existing
; Author ........: trancexx, modifyed by corgano
;
;==========================================================================================
Func _ExtractZip($sZipFile, $sDestinationFolder, $sFolderStructure = "")

    Local $i
    Do
        $i += 1
        $sTempZipFolder = @TempDir & "\Temporary Directory " & $i & " for " & StringRegExpReplace($sZipFile, ".*\\", "")
    Until Not FileExists($sTempZipFolder) ; this folder will be created during extraction

    Local $oShell = ObjCreate("Shell.Application")

    If Not IsObj($oShell) Then
        Return SetError(1, 0, 0) ; highly unlikely but could happen
    EndIf

    Local $oDestinationFolder = $oShell.NameSpace($sDestinationFolder)
    If Not IsObj($oDestinationFolder) Then
        DirCreate($sDestinationFolder)
;~         Return SetError(2, 0, 0) ; unavailable destionation location
    EndIf

    Local $oOriginFolder = $oShell.NameSpace($sZipFile & "\" & $sFolderStructure) ; FolderStructure is overstatement because of the available depth
    If Not IsObj($oOriginFolder) Then
        Return SetError(3, 0, 0) ; unavailable location
    EndIf

    Local $oOriginFile = $oOriginFolder.Items();get all items
    If Not IsObj($oOriginFile) Then
        Return SetError(4, 0, 0) ; no such file in ZIP file
    EndIf

    ; copy content of origin to destination
    $oDestinationFolder.CopyHere($oOriginFile, 20) ; 20 means 4 and 16, replaces files if asked

    DirRemove($sTempZipFolder, 1) ; clean temp dir

    Return 1 ; All OK!

EndFunc
Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites



In windows you can right click and extract files without any other software. could it use that?


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

do a search in the forum. this has already been discussed.

I did. I only found 2, one used DLL's and was much too big. The other had no download link.

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

That one has a link that says "skip previous releases and go to latest release", the post it brings me to looks like it had a link but it isn't there now.


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

torels UDF is BS.

You can use this function if it satisfies your needs:

DirCreate(@DesktopDir & "\Extracted") ; to extract to

_ExtractZip(@DesktopDir & "\YourZipFile.zip", "SomeFolderWithinOrNothing", "FileOrFolderToExtract.extensionIfEny", @DesktopDir & "\Extracted")

ConsoleWrite(@error & @CRLF)





; #FUNCTION# ;===============================================================================
;
; Name...........: _ExtractZip
; Description ...: Extracts file/folder from ZIP compressed file
; Syntax.........: _ExtractZip($sZipFile, $sFolderStructure, $sFile, $sDestinationFolder)
; Parameters ....: $sZipFile - full path to the ZIP file to process
;                  $sFolderStructure - 'path' to the file/folder to extract inside ZIP file
;                  $sFile - file/folder to extract
;                  $sDestinationFolder - folder to extract to. Must exist.
; Return values .: Success - Returns 1
;                          - Sets @error to 0
;                  Failure - Returns 0 sets @error:
;                  |1 - Shell Object creation failure
;                  |2 - Destination folder is unavailable
;                  |3 - Structure within ZIP file is wrong
;                  |4 - Specified file/folder to extract not existing
; Author ........: trancexx
;
;==========================================================================================
Func _ExtractZip($sZipFile, $sFolderStructure, $sFile, $sDestinationFolder)

    Local $i
    Do
        $i += 1
        $sTempZipFolder = @TempDir & "\Temporary Directory " & $i & " for " & StringRegExpReplace($sZipFile, ".*\\", "")
    Until Not FileExists($sTempZipFolder) ; this folder will be created during extraction

    Local $oShell = ObjCreate("Shell.Application")

    If Not IsObj($oShell) Then
        Return SetError(1, 0, 0) ; highly unlikely but could happen
    EndIf

    Local $oDestinationFolder = $oShell.NameSpace($sDestinationFolder)
    If Not IsObj($oDestinationFolder) Then
        Return SetError(2, 0, 0) ; unavailable destionation location
    EndIf

    Local $oOriginFolder = $oShell.NameSpace($sZipFile & "\" & $sFolderStructure) ; FolderStructure is overstatement because of the available depth
    If Not IsObj($oOriginFolder) Then
        Return SetError(3, 0, 0) ; unavailable location
    EndIf

    ;Local $oOriginFile = $oOriginFolder.Items.Item($sFile)
    Local $oOriginFile = $oOriginFolder.ParseName($sFile)
    If Not IsObj($oOriginFile) Then
        Return SetError(4, 0, 0) ; no such file in ZIP file
    EndIf

    ; copy content of origin to destination
    $oDestinationFolder.CopyHere($oOriginFile, 4) ; 4 means "do not display a progress dialog box", but apparently doesn't work

    DirRemove($sTempZipFolder, 1) ; clean temp dir

    Return 1 ; All OK!

EndFunc

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

That one has a link that says "skip previous releases and go to latest release", the post it brings me to looks like it had a link but it isn't there now.

Try reading past the link. Like at the bottom of the post where attached files always are.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

torels UDF is BS.

You can use this function if it satisfies your needs:

script goes here

Thanks! That is almost perfect, but it needs to extract all files in the file to the output folder. I am working on modifying it.

The UDF I am using now uses a DLL and is rather large.

*EDIT*

I was able to modify it to extract all files in the folder, and I removed a few of the things i didn't need. Very nice, a lot smaller than the one I was using (over 100 lines and a DLL to about 50 lines) Thank you all. here's the func

_ExtractZip("C:\Users\David\Desktop\New folder\Maps1.zip", "C:\Users\David\Desktop\New folder\maps")

; #FUNCTION# ;===============================================================================
;
; Name...........: _ExtractZip
; Description ...: Extracts file/folder from ZIP compressed file
; Syntax.........: _ExtractZip($sZipFile, $sDestinationFolder)
; Parameters ....: $sZipFile - full path to the ZIP file to process
;                  $sDestinationFolder - folder to extract to. Will be created if it does not exsist exist.
; Return values .: Success - Returns 1
;                          - Sets @error to 0
;                  Failure - Returns 0 sets @error:
;                  |1 - Shell Object creation failure
;                  |2 - Destination folder is unavailable
;                  |3 - Structure within ZIP file is wrong
;                  |4 - Specified file/folder to extract not existing
; Author ........: trancexx, modifyed by corgano
;
;==========================================================================================
Func _ExtractZip($sZipFile, $sDestinationFolder, $sFolderStructure = "")

    Local $i
    Do
        $i += 1
        $sTempZipFolder = @TempDir & "\Temporary Directory " & $i & " for " & StringRegExpReplace($sZipFile, ".*\\", "")
    Until Not FileExists($sTempZipFolder) ; this folder will be created during extraction

    Local $oShell = ObjCreate("Shell.Application")

    If Not IsObj($oShell) Then
        Return SetError(1, 0, 0) ; highly unlikely but could happen
    EndIf

    Local $oDestinationFolder = $oShell.NameSpace($sDestinationFolder)
    If Not IsObj($oDestinationFolder) Then
        DirCreate($sDestinationFolder)
;~         Return SetError(2, 0, 0) ; unavailable destionation location
    EndIf

    Local $oOriginFolder = $oShell.NameSpace($sZipFile & "\" & $sFolderStructure) ; FolderStructure is overstatement because of the available depth
    If Not IsObj($oOriginFolder) Then
        Return SetError(3, 0, 0) ; unavailable location
    EndIf

    Local $oOriginFile = $oOriginFolder.Items();get all items
    If Not IsObj($oOriginFile) Then
        Return SetError(4, 0, 0) ; no such file in ZIP file
    EndIf

    ; copy content of origin to destination
    $oDestinationFolder.CopyHere($oOriginFile, 20) ; 20 means 4 and 16, replaces files if asked

    DirRemove($sTempZipFolder, 1) ; clean temp dir

    Return 1 ; All OK!

EndFunc
Edited by corgano
2 people like this

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Maybe this little snippet you could use?

Add a second file status bar for files copied/files total?

A cool UDF huh?:D

EDIT: Remove the GUIRemove at the end, and only remove it when it's done?

#include <GUIConstants.au3>

_CopySingleFile(@ScriptFullPath, @DesktopDir & "\" & @ScriptName)


Func _CopySingleFile($source, $destination)
    if $source == "" or $destination == "" then
        SetError(1)
        Return 0
    EndIf
    $CopyWindow = GUICreate("Copy...", 369, 128, 193, 126)
    $Text = GUICtrlCreateLabel("", 8, 16,355, 25)
    $Progress1 = GUICtrlCreateProgress(8, 56, 353, 17)
    $btnProgress = GUICtrlCreateButton("Close", 136, 88, 75, 25, 0)
    GUICtrlSetState($btnProgress, $GUI_DISABLE)
    GUISetState(@SW_SHOW)
    Local $iProgress = FileGetSize($source)
    If $iProgress > 1024 Then $iProgress = $iProgress / 1024
    Local $x, $pos, $retv
        $hSource = FileOpen($source, 0)
         if $hSource = 0 then Return SetError(2)
         $hDest = FileOpen($destination, 10)
         if $hDest = 0  Then
             FileClose($hSource)
             Return SetError(3)
         EndIf
         GUICtrlSetData($text, "Copying '" & $source & "' to '" & $destination & "...")
         While 1
             $hSourceData = FileRead($hSource, 1024)
             if @error = -1 then
                 $retv = FileWrite($hDest,($hSourceData))
                 GUICtrlSetData($text, "Done.")
                 ExitLoop
             EndIf
             $pos = $pos + (100 / $iProgress)
             GUICtrlSetData($Progress1, $pos)
             if $iProgress < 100 Then sleep(10)
             $retv = FileWrite($hDest,($hSourceData))
        WEnd
        FileClose($hSource)
        FileClose($hDest)
        GUICtrlSetState($btnProgress, $GUI_ENABLE)
        While 1
            if GUIGetMsg() == $GUI_EVENT_CLOSE or GUIGetMsg() == $btnProgress Then ExitLoop
            sleep(10)
        WEnd
     GUIDelete($CopyWindow)
 EndFunc
Edited by DarkShadow6

Share this post


Link to post
Share on other sites

Hello...

Im posting here because i dont want open a new topic...

I read all topics but i dont know much of autoit, so, if you can, can anyone of you say me only if is possible extract one file giving only the directory and name of the zip?

For exemple...

$ZipDirectory = @DesktopDir
$ZipName = @DesktopDir

"the command"($ZipDirectory & "\" & ZipName (for give the directory and name of zip), @DesktopDir(the directory where the files will be pasted

If anyone can help me, thank you...

Share this post


Link to post
Share on other sites

Hi

Can you explain this part? I have a feeling this is a couple of lines of code embedded into one line, but maybe it'd be easier to understand if it was multiple lines of code?

Do

$i += 1

$sTempZipFolder = @TempDir & "\Temporary Directory " & $i & " for " & StringRegExpReplace($sZipFile, ".*\\", "")

From what I can gather, variable = temp directory location to extract to, not sure why the $i += 1 is there or the for in text, rather than as a statement.

Then you tell it to replace the zip file path, that has the .*\\ pattern (don't understand the .*\\) and replace it with nothing or the "" ?

Am I understanding any of this?

Thanks!

Thanks! That is almost perfect, but it needs to extract all files in the file to the output folder. I am working on modifying it.

The UDF I am using now uses a DLL and is rather large.

*EDIT*

I was able to modify it to extract all files in the folder, and I removed a few of the things i didn't need. Very nice, a lot smaller than the one I was using (over 100 lines and a DLL to about 50 lines) Thank you all. here's the func

_ExtractZip("C:\Users\David\Desktop\New folder\Maps1.zip", "C:\Users\David\Desktop\New folder\maps")

; #FUNCTION# ;===============================================================================
;
; Name...........: _ExtractZip
; Description ...: Extracts file/folder from ZIP compressed file
; Syntax.........: _ExtractZip($sZipFile, $sDestinationFolder)
; Parameters ....: $sZipFile - full path to the ZIP file to process
;                  $sDestinationFolder - folder to extract to. Will be created if it does not exsist exist.
; Return values .: Success - Returns 1
;                          - Sets @error to 0
;                  Failure - Returns 0 sets @error:
;                  |1 - Shell Object creation failure
;                  |2 - Destination folder is unavailable
;                  |3 - Structure within ZIP file is wrong
;                  |4 - Specified file/folder to extract not existing
; Author ........: trancexx, modifyed by corgano
;
;==========================================================================================
Func _ExtractZip($sZipFile, $sDestinationFolder, $sFolderStructure = "")

    Local $i
    Do
        $i += 1
        $sTempZipFolder = @TempDir & "\Temporary Directory " & $i & " for " & StringRegExpReplace($sZipFile, ".*\\", "")
    Until Not FileExists($sTempZipFolder) ; this folder will be created during extraction

    Local $oShell = ObjCreate("Shell.Application")

    If Not IsObj($oShell) Then
        Return SetError(1, 0, 0) ; highly unlikely but could happen
    EndIf

    Local $oDestinationFolder = $oShell.NameSpace($sDestinationFolder)
    If Not IsObj($oDestinationFolder) Then
        DirCreate($sDestinationFolder)
;~         Return SetError(2, 0, 0) ; unavailable destionation location
    EndIf

    Local $oOriginFolder = $oShell.NameSpace($sZipFile & "\" & $sFolderStructure) ; FolderStructure is overstatement because of the available depth
    If Not IsObj($oOriginFolder) Then
        Return SetError(3, 0, 0) ; unavailable location
    EndIf

    Local $oOriginFile = $oOriginFolder.Items();get all items
    If Not IsObj($oOriginFile) Then
        Return SetError(4, 0, 0) ; no such file in ZIP file
    EndIf

    ; copy content of origin to destination
    $oDestinationFolder.CopyHere($oOriginFile, 20) ; 20 means 4 and 16, replaces files if asked

    DirRemove($sTempZipFolder, 1) ; clean temp dir

    Return 1 ; All OK!

EndFunc

Share this post


Link to post
Share on other sites

Hi

Can you explain this part? I have a feeling this is a couple of lines of code embedded into one line, but maybe it'd be easier to understand if it was multiple lines of code?

From what I can gather, variable = temp directory location to extract to, not sure why the $i += 1 is there or the for in text, rather than as a statement.

Then you tell it to replace the zip file path, that has the .*\\ pattern (don't understand the .*\\) and replace it with nothing or the "" ?

Am I understanding any of this?

Thanks!

The idea is to find a folder name that isn't already used. So you make up a name which includes a number, and you keep looping increasing the number each time until the folder name doesn't exist.

In SRE '.' means any character. '.*' means any character any number of times. '.*\\' means any characters up to and including '\'. So you are replacing everything in the file name up to the last '\ with nothing (''). In other words, in case the full path to the file name was given you are extracting just the filename and removing the path.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Now I need to zip a file. There is .zip support included in windows so I know it should be possible to do it with only autoit and windows. Anyone know how?


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

Errm... I know. :)


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

LOL

Could you please tell me?

*edit*

or not.... I am gonna find a way around it

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

I'm still trying to make sense of your original code, I don't know much about object oriented stuff. I've put some psuedocode under each one, trying to make sense of it?

_ExtractZip("C:\Users\David\Desktop\New folder\Maps1.zip", "C:\Users\David\Desktop\New folder\maps")

; #FUNCTION# ;===============================================================================

;

; Name...........: _ExtractZip

; Description ...: Extracts file/folder from ZIP compressed file

; Syntax.........: _ExtractZip($sZipFile, $sDestinationFolder)

; Parameters ....: $sZipFile - full path to the ZIP file to process

; $sDestinationFolder - folder to extract to. Will be created if it does not exsist exist.

; Return values .: Success - Returns 1

; - Sets @error to 0

; Failure - Returns 0 sets @error:

; |1 - Shell Object creation failure

; |2 - Destination folder is unavailable

; |3 - Structure within ZIP file is wrong

; |4 - Specified file/folder to extract not existing

; Author ........: trancexx, modifyed by corgano

;

;==========================================================================================

Func _ExtractZip($sZipFile, $sDestinationFolder, $sFolderStructure = "")

Local $i

Do

$i += 1

$sTempZipFolder = @TempDir & "\Temporary Directory " & $i & " for " & StringRegExpReplace($sZipFile, ".*\\", "")

Until Not FileExists($sTempZipFolder) ; this folder will be created during extraction

^ I believe this tries to create a folder in the temp directory, with the name temporary directory 1 and if that is there, temporary directory 2, etc. and then it says for zip file name (with rest of path trimmed off)

Local $oShell = ObjCreate("Shell.Application")

If Not IsObj($oShell) Then

Return SetError(1, 0, 0) ; highly unlikely but could happen

EndIf

^ I believe this grabs the shell application, and if it is not there, gives an error? I don't know what the shell application part is for though

Local $oDestinationFolder = $oShell.NameSpace($sDestinationFolder)

If Not IsObj($oDestinationFolder) Then

DirCreate($sDestinationFolder)

;~ Return SetError(2, 0, 0) ; unavailable destionation location

EndIf

^ This appears to be setting the variable oDestinationFolder to a modified version of sDestinationFolder, but I'm not sure why? Then it checks to see if oDestinationFolder is an object and if it is not, it creates sDestinationFolder

Local $oOriginFolder = $oShell.NameSpace($sZipFile & "\" & $sFolderStructure) ; FolderStructure is overstatement because of the available depth

If Not IsObj($oOriginFolder) Then

Return SetError(3, 0, 0) ; unavailable location

EndIf

^ Here is appears that oOriginFolder is set to whatever namespace does to the zipfile\folder structure

Local $oOriginFile = $oOriginFolder.Items();get all items

If Not IsObj($oOriginFile) Then

Return SetError(4, 0, 0) ; no such file in ZIP file

EndIf

; copy content of origin to destination

^ This sets the value of oOriginFile to something?

$oDestinationFolder.CopyHere($oOriginFile, 20) ; 20 means 4 and 16, replaces files if asked

DirRemove($sTempZipFolder, 1) ; clean temp dir

Return 1 ; All OK!

EndFunc

^ This is some sort of file copy command, then it removes the temp directory

How does any of this unzip files? Is this confusing to me, because I don't know much about object oriented stuff?

LOL

Could you please tell me?

*edit*

or not.... I am gonna find a way around it

Share this post


Link to post
Share on other sites

This uses COM to ask the built in windows unzipper to unzip the file

But how do I zip a file?


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

gte asked you for a help. Your reply was what??


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0