Normand Briere
2019-09-30 3966454055db8e04700e881a091c2d33dcfda232
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
 
import java.io.*;
import java.awt.*;
import java.net.URL;
import java.util.zip.ZipInputStream;
 
public class cTools
{
    static byte[] bytes = new byte[16384];
 
    static private void GetRemoteZip(String url, String dir, String id, String name, String icon)
    {
        String filename = name + "_" + id;
        
        String location = dir + "/" + filename;
 
        java.net.URL u;
        InputStream is = null;
        
        if (new File(location).exists())
        {
            // Already downloaded
            System.out.println(location + ": Already downloaded");
            
            GetIcon(icon, location, "icon");
            
            return;
        }
        
        java.util.zip.ZipInputStream zis;
 
        String modelName = null;
        
        try
        {
            u = new java.net.URL(url + id);
            is = u.openStream();
 
            zis = new java.util.zip.ZipInputStream(is);
            
            new java.io.File(location).mkdirs();
 
            // now iterate through each item in the stream. The get next
            // entry call will return a ZipEntry for each file in the stream
            java.util.zip.ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null)
            {
                String entryName = entry.getName().toLowerCase();
                
                if (entryName.endsWith(".gsm"))
                {
                    // ArchiCAD
                    continue;
                }
 
                if (entryName.endsWith(".max"))
                {
                    // 3DS MAX
                    continue;
                }
 
                if (entryName.endsWith(".3ds") || entryName.endsWith(".obj"))
                {
                    modelName = entry.getName();
                }
 
                String s = String.format("Entry: %s len %d added %TD",
                        entry.getName(), entry.getSize(),
                        new java.util.Date(entry.getTime()));
                System.out.println(s);
 
                // Once we get the entry from the stream, the stream is
                // positioned read to read the raw data, and we keep
                // reading until read returns 0 or less.
                String outpath = location + "/" + entry.getName();
                
                //new java.io.File(outpath).mkdirs();
            
                TransferFile(outpath, zis);
            }
        } catch (java.net.MalformedURLException mue)
        {
            System.err.println("Ouch - a MalformedURLException happened.");
            mue.printStackTrace();
            //System.exit(2);
        } catch (IOException ioe)
        {
            System.err.println("Oops - an IOException happened.");
            ioe.printStackTrace();
            //System.exit(3);
        } catch (IllegalArgumentException iae)
        {
            System.err.println("Oops - an IllegalArgumentException happened.");
            iae.printStackTrace();
            //System.exit(3);
        } finally
        {
            try
            {
                if (is != null)
                {
                    is.close();
                }
            } catch (IOException ioe)
            {
            }
        }
 
        //  System.out.println("length = " + total);
 
//                try
//                {
//                    Runtime.getRuntime().exec("/usr/local/bin/wget https://archive3d.net/?a=download&do=get&id=7caca905");
//                }
//                catch (Exception e)
//                {
//                    e.printStackTrace();
//                }
        java.awt.image.BufferedImage image;
        
        try
        {
            u = new java.net.URL(icon);
            is = u.openStream();
            
//            image = (java.awt.image.BufferedImage)javax.imageio.ImageIO.read(u);
//            
//            //String[] g = javax.imageio.ImageIO.getWriterFormatNames();
//            
//            javax.imageio.ImageIO.write(image, "jpg", new File(location + "/" + name + id + ".jpg"));
            
            modelName = modelName.substring(0, modelName.length() - 4);
            
            TransferFile(location + "/" + modelName + ".jpg", is);
            
            GetIcon(icon, location, "icon");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
 
    private static void GetIcon(String icon, String location, String iconName)
    {
        URL u;
        InputStream is;
        try
        {
            //icon = https://storage3d.com/storage/2008.08/resized/7142f85f2575b35078f15feddaf8b315_64x64.jpg
            
            String[] split = icon.split("resized/");
            
            icon = split[0] + split[1];
            
            split = icon.split("_64x64");
            
            icon = split[0] + ".jpg";
            
            u = new java.net.URL(icon);
            is = u.openStream();
 
            TransferFile(location + "/" + iconName + ".jpg", is);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
 
    static private void TransferFile(String outpath, InputStream zis) throws IOException
    {
        FileOutputStream output = null;
        try
        {
            output = new FileOutputStream(outpath);
            int len = 0;
            while ((len = zis.read(bytes)) > 0)
            {
                output.write(bytes, 0, len);
            }
        } finally
        {
            // we must always close the output file
            if (output != null)
            {
                output.close();
            }
        }
    }
 
    static void Archive(javax.swing.JFrame frame)
    {
        FileDialog browser = new FileDialog(frame, "Select archive to extract...", FileDialog.LOAD);
        browser.setVisible(true);
        String filename = browser.getFile();
        if (filename != null && filename.length() > 0)
        {
            try
            {
                RandomAccessFile file = new RandomAccessFile(browser.getDirectory() + filename, "r");
                String str;
                while ((str = file.readLine()) != null)
                {
                    System.out.println(str);
                    
                    String cat = "nocat";
                    
                    String[] split = str.split("category=");
                    
                    if (split.length > 1)
                    {
                        String[] split2 = split[1].split(":");
                        
                        cat = split2[0];
                        
                        String[] split3 = cat.split("&");
                        
                        cat = split3[0];
                        
                        ////
                        str = split2[1];
                        
                        int i = 2;
                        while (i < split2.length)
                        {
                            str += ":" + split2[i++];
                        }
                    }
                    
                    split = str.split("id=");
                    
                    str = split[1];
                    split = str.split("\" title=\"Download ");
                    
                    String id = split[0];
                    
                    str = split[1];
                    split = str.split(" 3D Model\"><img src=\"");
                    
                    String name = split[0];
                    
                    str = split[1];
                    split = str.split("\" alt");
                    
                    String icon = split[0];
                    
                    GetRemoteZip("https://archive3d.net/?a=download&do=get&id=", browser.getDirectory() + "/" + cat, id, name, icon);
                    
                    // To avoid network overload.
                    Thread.sleep(1000);
                }
                
                file.close();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}