creating empty dirs

This commit is contained in:
Dmitry Avdeev
2012-10-11 14:00:05 +04:00
parent 857b7490ad
commit 3c8f18bc04
3 changed files with 27 additions and 28 deletions
@@ -51,6 +51,7 @@ import java.util.zip.ZipInputStream;
*/
public class ArchivedProjectTemplate implements ProjectTemplate {
static final String DESCRIPTION_PATH = ".idea/description.html";
private final String myDisplayName;
private final URL myArchivePath;
private final WizardContext myContext;
@@ -76,7 +77,7 @@ public class ArchivedProjectTemplate implements ProjectTemplate {
ZipInputStream stream = getStream();
ZipEntry entry;
while ((entry = stream.getNextEntry()) != null) {
if (entry.getName().endsWith("/description.html")) {
if (entry.getName().endsWith(DESCRIPTION_PATH)) {
return StreamUtil.readText(stream);
}
}
@@ -20,6 +20,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.StreamUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
@@ -29,6 +30,7 @@ import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
@@ -41,7 +43,7 @@ public class SaveProjectAsTemplateAction extends AnAction {
public void actionPerformed(AnActionEvent e) {
Project project = getEventProject(e);
assert project != null;
VirtualFile descriptionFile = VfsUtil.findRelativeFile(project.getBaseDir(), "description.html");
VirtualFile descriptionFile = VfsUtil.findRelativeFile(project.getBaseDir(), ArchivedProjectTemplate.DESCRIPTION_PATH.split("/"));
SaveProjectAsTemplateDialog dialog = new SaveProjectAsTemplateDialog(project, descriptionFile);
if (dialog.showAndGet()) {
File file = dialog.getTemplateFile();
@@ -51,6 +53,15 @@ public class SaveProjectAsTemplateAction extends AnAction {
file.createNewFile();
stream = new ZipOutputStream(new FileOutputStream(file));
VirtualFile dir = project.getBaseDir();
String description = dialog.getDescription();
if (descriptionFile == null) {
stream.putNextEntry(new ZipEntry(dir.getName() + "/" + ArchivedProjectTemplate.DESCRIPTION_PATH));
stream.write(description.getBytes());
stream.closeEntry();
}
else {
VfsUtil.saveText(descriptionFile, description);
}
ZipUtil.addDirToZipRecursively(stream, null, new File(dir.getPath()), dir.getName(), new FileFilter() {
@Override
public boolean accept(File pathname) {
@@ -59,6 +70,8 @@ public class SaveProjectAsTemplateAction extends AnAction {
return true;
}
}, null);
Messages.showInfoMessage(FileUtil.getNameWithoutExtension(file) + " was successfully created.\n" +
"It's available now in Project Wizard", "Template Created");
}
catch (IOException ex) {
Messages.showErrorDialog(project, ex.getMessage(), "Error");
@@ -99,17 +99,12 @@ public class ZipUtil {
@NotNull final ZipEntry zipEntry,
@NotNull final File extractToDir,
ZipInputStream stream) throws IOException {
final char pathDelimiterChar = '/';
String relativeExtractPath = createRelativeExtractPath(zipEntry, pathDelimiterChar, true);
int ind = relativeExtractPath.lastIndexOf(pathDelimiterChar);
final String relativeParentDir;
final String name;
if (ind != -1) {
relativeParentDir = relativeExtractPath.substring(0, ind);
name = relativeExtractPath.substring(ind + 1);
} else {
relativeParentDir = "";
name = relativeExtractPath;
String relativeExtractPath = createRelativeExtractPath(zipEntry);
File child = new File(extractToDir, relativeExtractPath);
File dir = zipEntry.isDirectory() ? child : child.getParentFile();
if (!dir.exists() && !dir.mkdirs()) {
throw new IOException("Unable to create dir: '" + dir + "'!");
}
if (zipEntry.isDirectory()) {
return;
@@ -117,14 +112,6 @@ public class ZipUtil {
if (progress != null) {
progress.setText("Extracting " + relativeExtractPath + " ...");
}
File parentDir = new File(extractToDir, relativeParentDir);
if (!parentDir.exists() || !parentDir.isDirectory()) {
boolean created = parentDir.mkdirs();
if (!created) {
throw new RuntimeException("Unable to create dir: '" + parentDir + "'!");
}
}
File child = new File(parentDir, name);
FileOutputStream fileOutputStream = new FileOutputStream(child);
try {
FileUtil.copy(stream, fileOutputStream);
@@ -134,15 +121,13 @@ public class ZipUtil {
LOG.info("Extract: " + relativeExtractPath);
}
private static String createRelativeExtractPath(ZipEntry zipEntry, char pathDelimiterChar, boolean singleTopLevelDir) {
private static String createRelativeExtractPath(ZipEntry zipEntry) {
String name = zipEntry.getName();
if (singleTopLevelDir) {
int ind = name.indexOf(pathDelimiterChar);
if (ind >= 0) {
name = name.substring(ind + 1);
}
int ind = name.indexOf('/');
if (ind >= 0) {
name = name.substring(ind + 1);
}
return StringUtil.trimEnd(name, String.valueOf(pathDelimiterChar));
return StringUtil.trimEnd(name, "/");
}
}