123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using log4net;
- namespace OpenSim.Framework.Serialization
- {
-
-
-
- public class TarArchiveWriter
- {
- protected static ASCIIEncoding m_asciiEncoding = new ASCIIEncoding();
- protected static UTF8Encoding m_utf8Encoding = new UTF8Encoding();
-
-
-
- protected BinaryWriter m_bw;
- public TarArchiveWriter(Stream s)
- {
- m_bw = new BinaryWriter(s);
- }
-
-
-
-
- public void WriteDir(string dirName)
- {
-
- if (!dirName.EndsWith("/"))
- dirName += "/";
- WriteFile(dirName, new byte[0]);
- }
-
-
-
-
-
- public void WriteFile(string filePath, string data)
- {
- WriteFile(filePath, m_utf8Encoding.GetBytes(data));
- }
-
-
-
-
-
- public void WriteFile(string filePath, byte[] data)
- {
- if (filePath.Length > 100)
- WriteEntry("././@LongLink", m_asciiEncoding.GetBytes(filePath), 'L');
- char fileType;
- if (filePath.EndsWith("/"))
- {
- fileType = '5';
- }
- else
- {
- fileType = '0';
- }
- WriteEntry(filePath, data, fileType);
- }
-
-
-
-
-
- public void Close()
- {
-
-
- byte[] finalZeroPadding = new byte[1024];
- lock (m_bw)
- {
- m_bw.Write(finalZeroPadding);
-
- m_bw.Flush();
- m_bw.Close();
- }
- }
- public static byte[] ConvertDecimalToPaddedOctalBytes(int d, int padding)
- {
- string oString = "";
- while (d > 0)
- {
- oString = Convert.ToString((byte)'0' + d & 7) + oString;
- d >>= 3;
- }
- while (oString.Length < padding)
- {
- oString = "0" + oString;
- }
- byte[] oBytes = m_asciiEncoding.GetBytes(oString);
- return oBytes;
- }
-
-
-
-
-
-
- protected void WriteEntry(string filePath, byte[] data, char fileType)
- {
-
- byte[] header = new byte[512];
-
- byte[] nameBytes = m_asciiEncoding.GetBytes(filePath);
- int nameSize = (nameBytes.Length >= 100) ? 100 : nameBytes.Length;
- Array.Copy(nameBytes, header, nameSize);
-
- byte[] modeBytes = m_asciiEncoding.GetBytes("0000777");
- Array.Copy(modeBytes, 0, header, 100, 7);
-
- byte[] ownerIdBytes = m_asciiEncoding.GetBytes("0000764");
- Array.Copy(ownerIdBytes, 0, header, 108, 7);
-
- byte[] groupIdBytes = m_asciiEncoding.GetBytes("0000764");
- Array.Copy(groupIdBytes, 0, header, 116, 7);
-
- int fileSize = data.Length;
-
- byte[] fileSizeBytes = ConvertDecimalToPaddedOctalBytes(fileSize, 11);
- Array.Copy(fileSizeBytes, 0, header, 124, 11);
-
- byte[] lastModTimeBytes = m_asciiEncoding.GetBytes("11017037332");
- Array.Copy(lastModTimeBytes, 0, header, 136, 11);
-
- header[156] = m_asciiEncoding.GetBytes(new char[] { fileType })[0];
- Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 329, 7);
- Array.Copy(m_asciiEncoding.GetBytes("0000000"), 0, header, 337, 7);
-
- Array.Copy(m_asciiEncoding.GetBytes(" "), 0, header, 148, 8);
- int checksum = 0;
- foreach (byte b in header)
- {
- checksum += b;
- }
-
- byte[] checkSumBytes = ConvertDecimalToPaddedOctalBytes(checksum, 6);
- Array.Copy(checkSumBytes, 0, header, 148, 6);
- header[154] = 0;
- lock (m_bw)
- {
-
- m_bw.Write(header);
-
-
-
- if (data.Length > 0)
- m_bw.Write(data);
-
- if (data.Length % 512 != 0)
- {
- int paddingRequired = 512 - (data.Length % 512);
-
-
-
- byte[] padding = new byte[paddingRequired];
- m_bw.Write(padding);
- }
- }
- }
- }
- }
|