getInputStream() public method

public getInputStream ( java arg0 ) : global::java.io.InputStream
arg0 java
return global::java.io.InputStream
Ejemplo n.º 1
2
        private Stream ReadDataFile( FileInfo dataFile )
        {
            Stream stream;
            if( dataFile.Extension == _CsvExt )
            {
                stream = dataFile.OpenRead();
            }
            else if( dataFile.Extension == _ZipExt )
            {
                if( dataFile.Length == 0 )
                    throw new Exception( "DataFile size is 0 bytes." );

                //раззиповать файл
                string csvFileName = Path.ChangeExtension( dataFile.FullName, ".csv" );
                ZipFile zf = new ZipFile( dataFile.FullName );
                try
                {
                    string csvEntry = dataFile.GetNameOnly() + _CsvExt;
                    ZipEntry ze = zf.getEntry( csvEntry );
                    if( ze == null ) throw new Exception(
                         csvEntry + " entry not found in " + dataFile.Name );
                    var zipStream = zf.getInputStream( ze );
                    try
                    {
                        using( FileStream stm = new FileStream( csvFileName, FileMode.Create, FileAccess.Write ) )
                        {
                            int len;
                            byte[] buf = new byte[ _BufferSize ];
                            sbyte[] sbuf = new sbyte[ _BufferSize ];
                            while( ( len = zipStream.read( sbuf ) ) > 0 )
                            {
                                Buffer.BlockCopy( sbuf, 0, buf, 0, len );
                                stm.Write( buf, 0, len );
                            }
                        }
                    }
                    finally
                    {
                        zipStream.close();
                    }
                }
                finally
                {
                    zf.close();
                }

                //прочитать раззипованный файл
                stream = new FileStream( csvFileName, FileMode.Open, FileAccess.Read );
            }
            else
            {
                throw new Exception( "Unknown data file extension: " + dataFile.Extension );
            }

            return stream;
        }
Ejemplo n.º 2
1
        public void UnZip(string zipFileLocation, string destinationRootFolder, string zipRootToRemove)
        {
            try
            {
                var zipFile = new ZipFile(zipFileLocation);
                var zipFileEntries = zipFile.entries();
                
                while (zipFileEntries.hasMoreElements())
                {
                    var zipEntry = (ZipEntry)zipFileEntries.nextElement();

                    var name = zipEntry.getName().Replace(zipRootToRemove, "").Replace("/", "\\").TrimStart('/').TrimStart('\\');
                    var p = this.fileSystem.Path.Combine(destinationRootFolder, name);

                    if (zipEntry.isDirectory())
                    {
                        if (!this.fileSystem.Directory.Exists(p))
                        {
                            this.fileSystem.Directory.CreateDirectory(p);
                        };
                    }
                    else
                    {
                        using (var bis = new BufferedInputStream(zipFile.getInputStream(zipEntry)))
                        {
                            var buffer = new byte[2048];
                            var count = buffer.GetLength(0);
                            using (var fos = new FileOutputStream(p))
                            {
                                using (var bos = new BufferedOutputStream(fos, count))
                                {
                                    int size;
                                    while ((size = bis.read(buffer, 0, count)) != -1)
                                    {
                                        bos.write(buffer, 0, size);
                                    }

                                    bos.flush();
                                    bos.close();
                                }
                            }

                            bis.close();
                        }
                    }
                }

                zipFile.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (Exception e)
            {
                var t = e.ToString();
            }
        }
Ejemplo n.º 3
0
        private void ExtractFile(ZipFile zipFile, ZipEntry zipEntry, string tempFileName)
        {
            InputStream s = zipFile.getInputStream(zipEntry);
            try
            {
                FileOutputStream dest = new FileOutputStream(tempFileName);
                try
                {
                    int len = 0;
                    sbyte[] buffer = new sbyte[7168];
                    while ((len = s.read(buffer)) >= 0)
                    {
                        dest.write(buffer, 0, len);
                    }
                }
                finally
                {
                    dest.close();
                }
            }
            finally
            {
                s.close();
            }

        }
Ejemplo n.º 4
0
 public static void Unzip(string zipFile, string[] targetFiles, string output)
 {
     java.util.zip.ZipFile file = new java.util.zip.ZipFile(zipFile);
     try
     {
         using (List<ZipEntry>.Enumerator enumerator = GetZippedItems(file).GetEnumerator())
         {
             Predicate<string> match = null;
             ZipEntry entry;
             while (enumerator.MoveNext())
             {
                 entry = enumerator.Current;
                 if ((targetFiles != null) && (targetFiles.Length > 0))
                 {
                     if (match == null)
                     {
                         match = delegate (string s) {
                             return entry.getName().ToLower().EndsWith(s.ToLower().Replace(@"\", "/").TrimStart("/".ToCharArray()));
                         };
                     }
                     if (Array.FindIndex<string>(targetFiles, match) < 0)
                     {
                         continue;
                     }
                 }
                 if (!entry.isDirectory())
                 {
                     InputStream source = file.getInputStream(entry);
                     try
                     {
                         string fileName;
                         string directoryName;
                         if (Path.HasExtension(output) && !Directory.Exists(output))
                         {
                             fileName = Path.GetFileName(output);
                             directoryName = Path.GetDirectoryName(output);
                         }
                         else
                         {
                             fileName = Path.GetFileName(entry.getName());
                             directoryName = Path.GetDirectoryName(entry.getName());
                             directoryName = output + @"\" + directoryName;
                         }
                         Directory.CreateDirectory(directoryName);
                         FileOutputStream destination = new FileOutputStream(Path.Combine(directoryName, fileName));
                         try
                         {
                             CopyStream(source, destination);
                         }
                         finally
                         {
                             destination.close();
                         }
                         continue;
                     }
                     finally
                     {
                         source.close();
                     }
                 }
             }
         }
     }
     finally
     {
         if (file != null)
         {
             file.close();
         }
     }
 }
Ejemplo n.º 5
0
 public bool FromLocalFile(string file, out IAdditionalContent content)
 {
     if (!base.ContentType.CurrentUserCanUpload)
     {
         content = null;
         return false;
     }
     try
     {
         string tempFileName;
         string str6;
         string str7;
         string extension = Path.GetExtension(file);
         XmlDocument document = null;
         switch (extension)
         {
             case ".scd":
             case ".zip":
             {
                 ZipFile file2 = null;
                 try
                 {
                     file2 = new ZipFile(file);
                     Enumeration enumeration = file2.entries();
                     List<ZipEntry> list = new List<ZipEntry>();
                     while (enumeration.hasMoreElements())
                     {
                         list.Add(enumeration.nextElement() as ZipEntry);
                     }
                     foreach (ZipEntry entry in list)
                     {
                         if (!entry.isDirectory())
                         {
                             this.NumberOfFiles++;
                         }
                         if (entry.getName().ToLower().EndsWith("mod_info.lua"))
                         {
                             string path = entry.getName();
                             string directoryName = Path.GetDirectoryName(path);
                             if ((directoryName == null) || (directoryName.Length < 1))
                             {
                                 directoryName = Path.GetDirectoryName(file);
                             }
                             this.mModName = new DirectoryInfo(directoryName).Name;
                             path = Path.GetFileName(path);
                             InputStream stream = null;
                             try
                             {
                                 stream = file2.getInputStream(entry);
                                 byte[] bytes = new byte[0x1000];
                                 bool flag = false;
                                 string contents = "";
                                 while (!flag)
                                 {
                                     int index = 0;
                                     while (index < bytes.Length)
                                     {
                                         int num = stream.read();
                                         if (num < 0)
                                         {
                                             flag = true;
                                             break;
                                         }
                                         bytes[index] = (byte) num;
                                         index++;
                                     }
                                     contents = contents + Encoding.UTF8.GetString(bytes, 0, index);
                                 }
                                 tempFileName = Path.GetTempFileName();
                                 System.IO.File.WriteAllText(tempFileName, contents);
                                 str7 = LuaUtil.ModToXml(tempFileName, out str6);
                                 if ((str6 != null) && (str6.Length > 0))
                                 {
                                     ErrorLog.WriteLine("Error parsing Mod file: {0}", new object[] { str6 });
                                     content = null;
                                     return false;
                                 }
                                 document = new XmlDocument();
                                 document.LoadXml(str7);
                                 base.LocalFilePath = file;
                                 System.IO.File.Delete(tempFileName);
                             }
                             finally
                             {
                                 if (stream != null)
                                 {
                                     stream.close();
                                 }
                             }
                         }
                     }
                 }
                 finally
                 {
                     file2.close();
                 }
                 break;
             }
             default:
                 if (extension == ".lua")
                 {
                     if (!file.EndsWith("mod_info.lua"))
                     {
                         content = null;
                         return false;
                     }
                     str7 = LuaUtil.ModToXml(file, out str6);
                     if ((str6 != null) && (str6.Length > 0))
                     {
                         ErrorLog.WriteLine("Error parsing Mod file: {0}", new object[] { str6 });
                         content = null;
                         return false;
                     }
                     document = new XmlDocument();
                     document.LoadXml(str7);
                     base.LocalFilePath = Path.GetDirectoryName(file);
                     this.mNumberOfFiles = Directory.GetFiles(base.LocalFilePath, "*.*", SearchOption.AllDirectories).Length;
                     this.mModName = new DirectoryInfo(Path.GetDirectoryName(file)).Name;
                 }
                 break;
         }
         if (document == null)
         {
             content = null;
             return false;
         }
         base.Name = document["mod"]["name"].InnerText;
         base.Description = document["mod"]["description"].InnerText;
         if (document["mod"]["copyright"] != null)
         {
             this.Copyright = document["mod"]["copyright"].InnerText.Replace("\x00a9", "").Replace("\x00ef\x00bf\x00bd", "");
         }
         if (document["mod"]["author"] != null)
         {
             this.DeveloperName = document["mod"]["author"].InnerText;
         }
         if (document["mod"]["url"] != null)
         {
             this.Website = document["mod"]["url"].InnerText;
         }
         this.Exclusive = this.ParseBool(document["mod"]["exclusive"].InnerText);
         this.UIOnly = new bool?(this.ParseBool(document["mod"]["ui_only"].InnerText));
         this.Guid = document["mod"]["uid"].InnerText;
         if (document["mod"]["requires"] != null)
         {
             this.mRequirements = document["mod"]["requires"].InnerText;
         }
         if (document["mod"]["requiresNames"] != null)
         {
             this.mRequirementNames = document["mod"]["requiresNames"].InnerText;
         }
         if (document["mod"]["conflicts"] != null)
         {
             this.mConflicts = document["mod"]["conflicts"].InnerText;
         }
         if (((document["mod"]["icon"] != null) && (document["mod"]["icon"].InnerText != null)) && (document["mod"]["icon"].InnerText.Length > 0))
         {
             string targetFile = document["mod"]["icon"].InnerText.Replace("/", @"\");
             if (extension == ".lua")
             {
                 if (targetFile.IndexOf(this.ModName) >= 0)
                 {
                     targetFile = targetFile.Remove(0, targetFile.IndexOf(this.ModName)).TrimStart(@"\".ToCharArray());
                 }
                 string str9 = Path.GetDirectoryName(base.LocalFilePath);
                 string str10 = targetFile;
                 string str11 = Path.Combine(str9, str10);
                 if (Path.GetExtension(str11) == ".dds")
                 {
                     this.mPreviewImage50 = ConvertDDS.ToBitmap(str11);
                 }
                 else
                 {
                     this.mPreviewImage50 = Image.FromFile(str11);
                 }
             }
             else
             {
                 tempFileName = Path.GetTempFileName();
                 Compression.Unzip(file, targetFile, tempFileName);
                 if (Path.GetExtension(targetFile) == ".dds")
                 {
                     this.mPreviewImage50 = ConvertDDS.ToBitmap(tempFileName);
                 }
                 else
                 {
                     this.mPreviewImage50 = Image.FromFile(tempFileName);
                 }
                 System.IO.File.Delete(tempFileName);
             }
             this.mHasPreview = true;
         }
         else
         {
             this.mPreviewImage50 = null;
         }
         content = this;
         return true;
     }
     catch (Exception exception)
     {
         ErrorLog.WriteLine(exception);
         content = null;
         return false;
     }
 }
Ejemplo n.º 6
0
 public bool FromLocalFile(string file, out IAdditionalContent content)
 {
     Exception exception;
     if (!base.ContentType.CurrentUserCanUpload)
     {
         content = null;
         return false;
     }
     try
     {
         byte[] buffer;
         int num2;
         string extension = Path.GetExtension(file);
         string contents = "";
         XmlDocument document = null;
         switch (extension)
         {
             case ".scd":
             case ".zip":
             {
                 ZipFile file2 = null;
                 try
                 {
                     file2 = new ZipFile(file);
                     Enumeration enumeration = file2.entries();
                     List<ZipEntry> list = new List<ZipEntry>();
                     while (enumeration.hasMoreElements())
                     {
                         list.Add(enumeration.nextElement() as ZipEntry);
                     }
                     bool flag = false;
                     foreach (ZipEntry entry in list)
                     {
                         if (entry.getName().EndsWith("_scenario.lua"))
                         {
                             string fileName = Path.GetFileName(entry.getName());
                             this.mMapName = fileName.Remove(fileName.IndexOf("_scenario.lua"), "_scenario.lua".Length);
                             InputStream stream = null;
                             try
                             {
                                 string str5;
                                 stream = file2.getInputStream(entry);
                                 buffer = new byte[0x1000];
                                 bool flag2 = false;
                                 while (!flag2)
                                 {
                                     num2 = 0;
                                     while (num2 < buffer.Length)
                                     {
                                         int num = stream.read();
                                         if (num < 0)
                                         {
                                             flag2 = true;
                                             break;
                                         }
                                         buffer[num2] = (byte) num;
                                         num2++;
                                     }
                                     contents = contents + Encoding.UTF8.GetString(buffer, 0, num2);
                                 }
                                 string tempFileName = Path.GetTempFileName();
                                 System.IO.File.WriteAllText(tempFileName, contents);
                                 if (!LuaUtil.VerifyScenario(tempFileName, out str5))
                                 {
                                     ErrorLog.WriteLine("Error loading custom map file {0}: {1}", new object[] { Path.GetFileName(tempFileName), str5 });
                                     content = null;
                                     return false;
                                 }
                                 new XmlDocument().LoadXml(LuaUtil.ScenarioToXml(tempFileName));
                                 System.IO.File.Delete(tempFileName);
                                 base.LocalFilePath = file;
                                 flag = true;
                                 break;
                             }
                             finally
                             {
                                 if (stream != null)
                                 {
                                     stream.close();
                                 }
                             }
                         }
                     }
                     if (!flag)
                     {
                         content = null;
                         return false;
                     }
                 }
                 finally
                 {
                     file2.close();
                 }
                 goto Label_03C3;
             }
             default:
             {
                 if (!(extension == ".lua"))
                 {
                     goto Label_03C3;
                 }
                 if (!file.EndsWith("_scenario.lua"))
                 {
                     content = null;
                     return false;
                 }
                 this.mMapName = Path.GetFileName(file);
                 this.mMapName = this.mMapName.Remove(this.mMapName.IndexOf("_scenario.lua"), "_scenario.lua".Length);
                 document = new XmlDocument();
                 document.LoadXml(LuaUtil.ScenarioToXml(file));
                 FileStream stream2 = null;
                 for (num2 = 0; num2 < 3; num2++)
                 {
                     try
                     {
                         bool flag4;
                         base.LocalFilePath = Path.GetDirectoryName(file);
                         stream2 = System.IO.File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
                         buffer = new byte[0x1000];
                         int count = 0;
                         goto Label_034E;
                     Label_0316:
                         count = stream2.Read(buffer, 0, buffer.Length);
                         if (count < 1)
                         {
                             break;
                         }
                         contents = contents + Encoding.UTF8.GetString(buffer, 0, count);
                     Label_034E:
                         flag4 = true;
                         goto Label_0316;
                     }
                     catch (Exception exception1)
                     {
                         exception = exception1;
                         ErrorLog.WriteLine(exception);
                         Thread.Sleep(0x3e8);
                     }
                     finally
                     {
                         if (stream2 != null)
                         {
                             stream2.Close();
                         }
                     }
                 }
                 break;
             }
         }
         if ((contents == null) || (contents.Length < 1))
         {
             content = null;
             return false;
         }
     Label_03C3:
         if (document == null)
         {
             content = null;
             return false;
         }
         base.Name = document["scenario"]["name"].InnerText;
         this.mMapDescription = document["scenario"]["description"].InnerText;
         this.mWidth = int.Parse(document["scenario"]["size"]["width"].InnerText);
         this.mHeight = int.Parse(document["scenario"]["size"]["height"].InnerText);
         this.mSize = (this.Width + this.Height) / 2;
         this.mMaxPlayers = int.Parse(document["scenario"]["max_players"].InnerText);
         string innerText = document["scenario"]["map"].InnerText;
         if ((extension == ".scd") || (extension == ".zip"))
         {
             this.mPreviewImage256 = ExtractZippedImage(file, innerText);
         }
         else
         {
             this.mPreviewImage256 = ExtractImage(file, innerText);
         }
         this.mPreviewImage50 = null;
         this.mPreviewImage128 = null;
         content = this;
         return true;
     }
     catch (Exception exception2)
     {
         exception = exception2;
         ErrorLog.WriteLine(exception);
         content = null;
         return false;
     }
 }
Ejemplo n.º 7
0
 public static Image ExtractZippedImage(string zipFile, string mappath)
 {
     InputStream stream = null;
     ZipFile file = null;
     Image image;
     try
     {
         file = new ZipFile(zipFile);
         Enumeration enumeration = file.entries();
         List<ZipEntry> list = new List<ZipEntry>();
         while (enumeration.hasMoreElements())
         {
             list.Add(enumeration.nextElement() as ZipEntry);
         }
         foreach (ZipEntry entry in list)
         {
             if (entry.getName().EndsWith(Path.GetFileName(mappath)))
             {
                 stream = file.getInputStream(entry);
                 break;
             }
         }
         if (stream == null)
         {
             return ExtractImage(zipFile, mappath);
         }
         Bitmap bitmap = new Bitmap(0x100, 0x100);
         byte[] buffer = new byte[0x40000];
         int index = 0;
         stream.read(new sbyte[0xa5], 0, 0xa5);
         while (index < 0x40000)
         {
             for (int i = 0; i < 0x1000; i++)
             {
                 buffer[index + i] = (byte) stream.read();
             }
             index += 0x1000;
         }
         index = 0;
         int x = 0;
         int y = 0;
         while (index < 0x40000)
         {
             Color color = Color.FromArgb(buffer[index], buffer[index + 3], buffer[index + 2], buffer[index + 1]);
             bitmap.SetPixel(x, y, color);
             x++;
             if (x > 0xff)
             {
                 x = 0;
                 y++;
             }
             index += 4;
         }
         image = bitmap;
     }
     catch (Exception exception)
     {
         ErrorLog.WriteLine(exception);
         image = null;
     }
     finally
     {
         if (stream != null)
         {
             stream.close();
         }
         if (file != null)
         {
             file.close();
         }
     }
     return image;
 }