Sfoglia il codice sorgente

Fixed file type values. Added fileType arg to AddEntry().

Brucey 3 anni fa
parent
commit
6e29227bcc
3 ha cambiato i file con 51 aggiunte e 33 eliminazioni
  1. 7 7
      core.mod/common.bmx
  2. 41 24
      core.mod/core.bmx
  3. 3 2
      zip.mod/examples/example_01.bmx

+ 7 - 7
core.mod/common.bmx

@@ -137,13 +137,13 @@ Enum EArchiveFormat
 End Enum
 
 Enum EArchiveFileType:UInt
-	File = 100000 ' regular file
-	Link = 120000 ' symbolic link
-	Socket = 140000 ' socket
-	CharDevice = 20000 ' character device
-	BlockDevice = 60000 ' block device
-	Dir = 40000 ' directory
-	Fifo = 10000 ' named pipe
+	File = 32768 ' regular file - 0100000 (octal)
+	Link = 40960 ' symbolic link - 0120000
+	Socket = 49152 ' socket - 0140000
+	CharDevice = 8192 ' character device - 0020000
+	BlockDevice = 24576 ' block device - 0060000
+	Dir = 16384 ' directory - 0040000
+	Fifo = 4096 ' named pipe - 0010000
 End Enum
 
 Const ARCHIVE_FILTER_NONE:Int = 0

+ 41 - 24
core.mod/core.bmx

@@ -490,8 +490,12 @@ Type TWriteArchive Extends TArchive
 	bbdoc: Convience method for adding a file or #TStream to the archive.
 	about: If adding a #TStream, you will also need to provide @pathname, @size and @ftime values.
 	By default, files are added with the permission '0644' (decimal 420).
+	@pathname can also be used to define the path (include sub directories) of the file within the archive - this
+	is also dependent on support from the archive in question.
+
+	@fileType will 
 	End Rem
-	Method AddEntry(file:Object, pathname:String = Null, size:Long = 0, ftime:Long = 0)
+	Method AddEntry(file:Object, pathname:String = Null, size:Long = 0, ftime:Long = 0, fileType:EArchiveFileType = EArchiveFileType.File)
 		If String(file) Then
 			If Not pathname Then
 				pathname = StripDir(String(file))
@@ -506,42 +510,55 @@ Type TWriteArchive Extends TArchive
 			Throw New TArchiveException("Pathname is required. Unable to determine pathname from input")
 		End If
 
-		Local stream:TStream = ReadStream(file)
-
-		If Not stream Then
-			Throw New TArchiveException("Unabled to open stream for input file for path '" + pathname + "'")
+		If fileType <> EArchiveFileType.File And fileType <> EArchiveFileType.Dir Then
+			Throw New TArchiveException("fileType must be a File or a Dir.")
 		End If
 
-		If Not size Then
-			size = stream.Size()
+		Local stream:TStream
+		
+		If fileType = EArchiveFileType.File Then
+			stream = ReadStream(file)
+
+			If Not stream Then
+				Throw New TArchiveException("Unabled to open stream for input file for path '" + pathname + "'")
+			End If
+
+			If Not size Then
+				size = stream.Size()
+			End If
 		End If
 
 		Local entry:TArchiveEntry = New TArchiveEntry
 		entry.SetPathname(pathname)
-		entry.SetSize(size)
-		entry.SetFileType(EArchiveFileType.File)
-		entry.SetPermission(420) ' 0644
-		If ftime Then
-			entry.SetModifiedTime(ftime)
+		entry.SetFileType(fileType)
+
+		If fileType = EArchiveFileType.File Then
+			entry.SetPermission(420) ' 0644
+			entry.SetSize(size)
+			If ftime Then
+				entry.SetModifiedTime(ftime)
+			End If
 		End If
 
 		If Header(entry) <> ARCHIVE_OK Then
 			Throw New TArchiveException(String.FromUTF8String(archive_error_string(archivePtr)))
 		End If
 
-		Local StaticArray buf:Byte[8192]
-		Local count:Size_T = size
-		While count
-			Local nCount:Long = stream.Read(buf, Min(count, buf.Length))
-			If nCount <= 0 Then
-				Exit
+		If fileType = EArchiveFileType.File Then
+			Local StaticArray buf:Byte[8192]
+			Local count:Size_T = size
+			While count
+				Local nCount:Long = stream.Read(buf, Min(count, buf.Length))
+				If nCount <= 0 Then
+					Exit
+				End If
+				Data(buf, Size_T(nCount))
+				count :- nCount
+			Wend
+
+			If Not TStream(file) Then
+				stream.Close()
 			End If
-			Data(buf, Size_T(nCount))
-			count :- nCount
-		Wend
-
-		If Not TStream(file) Then
-			stream.Close()
 		End If
 
 		FinishEntry()

+ 3 - 2
zip.mod/examples/example_01.bmx

@@ -8,8 +8,9 @@ wa.SetFormat(EArchiveFormat.ZIP)
 
 wa.Open("data.zip")
 
-wa.AddEntry("testdata.txt")
-wa.AddEntry("테스트_데이터.txt")
+wa.AddEntry("testdata.txt", "files/testdata.txt")
+wa.AddEntry("테스트_데이터.txt", "files/테스트_데이터.txt")
+wa.AddEntry("", "empty", 0, 0, EArchiveFileType.Dir)
 
 wa.Close()